2

I want to throw an error after binding the form. Here my code:

$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'
            }

How can I do it in symfony2 after Method-Post?

craphunter
  • 961
  • 2
  • 13
  • 34
  • possible duplicate of [Adding a field specific error from the controller in symfony2](http://stackoverflow.com/questions/12170440/adding-a-field-specific-error-from-the-controller-in-symfony2) – craphunter Apr 07 '14 at 13:40

1 Answers1

7

Yes, you can do:

use Symfony\Component\Form\FormError;
//...
$dateError = new FormError("Age must be greater than 18");
$form->get('date')->addError($dateError);
Ramesh
  • 4,223
  • 2
  • 16
  • 24
  • Thanks, but how do I throw the error? Take a look here: http://stackoverflow.com/questions/25270157/command-to-throw-formerror. I appreciate an answer! – craphunter Aug 12 '14 at 17:36