1

For my contact form no form errors are displayed.

    $contact = new Contact();

    $form = $this->createForm(new ContactFormType(), $contact);

    /* handle contact form submission */
    if ('POST' == $request->getMethod()) {
        $form->bindRequest($request);

        if ($form->isValid()) {
            //Do something
        }
    }

    return $this->render('MainBundle:Default:contact.html.twig', array(
                'form' => $form->createView()
    ));

My validator

Fc\MainBundle\Entity\Contact:
    properties:
        firstName:
            - NotBlank: ~

        lastName:
            - NotBlank: ~

        email:
            - NotBlank: ~
            - Email:
                checkMX: true
        title:
            - NotBlank: ~
        message:
            - NotBlank: ~

I have added a novalidate attribute to my form tag to check if my validation works. Now when I submit the form with empty data nothing happens (with correct data everything is fine), the controller identifies the form as invalid and stops further processes.

In the template I call

 {{ form_errors(form) }}

But nothing is displayed. Any ideas why? I use symfony 2.1.X

1 Answer 1

5

Can you confirm that the validation is working with this:

if ($form->isValid()) {
//Do something
}
else
{ die('not valid') }

Is the form well posted?

Is there anything in the HTML that shows up where you call {{ form_errors(form) }} ? - it would be a CSS or JS problem if that's the case...

Have you tried to show the errors by field (there is no global error in your validation apparently):

{{ form_errors(form.firstName) }}
etc.

Remark: form.firstName works, not form.first_name. You have to follow that convention with Doctrine2/Symfony2.

Sign up to request clarification or add additional context in comments.

10 Comments

+1 Your last point...it shows the error message for the firstName field or any single field. But form_errors(form) does not work. I need form_errors(form) to work
can't remember the syntax then but basically, you just need to loop on all the form errors... have a look in the API. Probably something like $errors = $form->getErrors() and use that in the template (for error in errors ... error.message)
$form->getErrors() returns empty array
In the API: github.com/symfony/Form/blob/master/FormView.php ... FormView implements IteratorAggregate which means you can do {% for raw in form %} . form_errors(form) display global errors. form_errors(raw) should do.
I found out the answer to my problem. Symfony2 doesn't like "_" underscores in field names. I had to follow the camel convention and change it to firstName. Now it works for individual fields.
|

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.