I need to display validation errors in my form that extends sfForm. The problem is that when I send the invalid form I don't get validation errors displayed on my form. Here is my form:
class FinanceStatementOfWorkForm extends sfForm
{
public function configure()
{
$this->widgetSchema->setNameFormat('master_groups[%s]');
$finance_statement_id = $this->getOption('finance_statement_id');
$choices = Doctrine::getTable('FinanceTask')->getGroups($finance_statement_id);
$this->widgetSchema['master_start'] = new sfWidgetFormChoice(
array(
'choices' => $choices
)
);
$this->setDefault('master_start', Doctrine::getTable('FinanceTask')->getActiveMaster($finance_statement_id, $type = 'start'));
$this->validatorSchema['master_start'] = new sfValidatorChoice(array(
'choices'=> array_keys($choices)
));
$this->validatorSchema->setPostValidator(
new sfValidatorCallback(array('callback'=>array($this, 'checkGroups')))
);
$this->widgetSchema['master_end'] = new sfWidgetFormChoice(
array(
'choices' => $choices
)
);
$this->setDefault('master_end', Doctrine::getTable('FinanceTask')->getActiveMaster($finance_statement_id, $type = 'end'));
$this->validatorSchema['master_end'] = new sfValidatorChoice(array(
'choices'=> array_keys($choices)
));
$this->disableCSRFProtection();
}
public function checkGroups($validator, $values)
{
if ($values['master_start'] == $values['master_end'])
throw new sfValidatorError($validator, 'Could not be equal with master end group');
else
return $values;
}
}
Next in my template:
<fieldset>
<legend><?php echo __('Set Master Groups') ?></legend>
<form action="<?php echo url_for('@finance_projects\saveMasterGroups') ?>" method="post">
<?php echo $form ?>
<div class="w-box-b">
<div class="button-white button-save">
<span><button type="submit"><span><?php echo __('Save') ?></span></button></span>
</div>
</div>
</form>
</fieldset>
And then I process the form in the action:
public function executeSaveMasterGroups(sfWebRequest $request)
{
$this->form = new FinanceStatementOfWorkForm(array(), array(
'finance_statement_id'=>$this->finance_statement->id
));
$this->processSaveMasterGroupsForm($request, $this->form);
// $this->executeIndex($request);
// $this->setTemplate('index');
}
public function processSaveMasterGroupsForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid())
{
Doctrine::getTable('FinanceTask')->updateGroups($request->getParameter('master_groups'));
}
else
$this->getUser()->setFlashFormError(false);
}
If I set the template, I see a broken template (missing some variables from my executeIndex) with my form but with validation errors. If executing index before or just commenting out those 2 lines I get only flash message. How to fix that?
Found the solution, more attention to positioning the action calls.
public function executeSaveMasterGroups(sfWebRequest $request)
{
$this->executeIndex($request);
$this->processSaveMasterGroupsForm($request, $this->form);
$this->setTemplate('index');
}
public function processSaveMasterGroupsForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid())
{
Doctrine::getTable('FinanceTask')->updateGroups($request->getParameter('master_groups'));
}
else
$this->getUser()->setFlashFormError(false);
$this->form = $form;
}