绑定后Symfony2抛出表单错误

时间:2014-04-07 13:07:19

标签: forms validation symfony post

我想在绑定表单后抛出错误。这是我的代码:

$form = $this->createFormBuilder()
            ...
            ->add('date', 'birthday', array(
                'years' => range($year_18-90, $year_18),
                'empty_value' => array('year' => $year_18-16)
            ))->getForm;

//Post and valid 
if ($form->isValid()) {
            $formData = $form->getData();
            if ($formData['date']->getTimestamp() > $date_18) {
                //if user is under 18, then throw an error in from 'date' / ' birthday'
            }

如何在Method-Post之后在symfony2中执行此操作?

1 个答案:

答案 0 :(得分:6)

是的,你可以这样做:

use Symfony\Component\Form\FormError;
//...
$dateError = new FormError("Age must be greater than 18");
$form->get('date')->addError($dateError);