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?

1

1 Answer 1

7

Yes, you can do:

use Symfony\Component\Form\FormError;
//...
$dateError = new FormError("Age must be greater than 18");
$form->get('date')->addError($dateError);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but how do I throw the error? Take a look here: stackoverflow.com/questions/25270157/command-to-throw-formerror. I appreciate an answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.