我需要在扩展 sfForm 的表单中显示验证错误。问题是当我发送无效表单时,我的表单上没有显示验证错误。这是我的表格:
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;
}
}
接下来在我的模板中:
<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>
然后我在操作中处理表单:
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);
}
如果我设置了模板,我会在我的表单中看到一个损坏的模板(我的 executeIndex 中缺少一些变量),但有验证错误。如果在执行 index 之前或只是注释掉这 2 行,我只会收到 flash 消息。如何解决?