我想在绑定表单后抛出错误。这是我的代码:
$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中执行此操作?
答案 0 :(得分:6)
是的,你可以这样做:
use Symfony\Component\Form\FormError;
//...
$dateError = new FormError("Age must be greater than 18");
$form->get('date')->addError($dateError);