0

I am having trouble on Symfony3 to display {{ form_errors([formname.field]) }} messages with a form embedding other forms.

I've dropped a symfony project on GitHub to explain my issue: https://github.com/nyluje/symfony_test_form_embedded

You'll find in it:

  • \src\AppBundle\Controller\DefaultController.php modified to display the test.
  • Under \src\AppBundle\Entity\ two entities: EntityForFormA and EntityForFromB created with a "field1" being of EmailType.
  • Under \src\AppBundle\Form: 3 forms files; 2 Are directly related to the EntityForFormA and EntityForFormB: FormAType and FormBType, and the last one FormCType is there to embed both into one form.

At the top of the file \app\Resources\default\index.hmtl.twig:

{{ form_start(form_a) }}
    {{ form_label(form_a.field1) }}
    {{ form_widget(form_a.field1) }}
    {{ form_errors(form_a.field1) }}
{{ form_end(form_a) }}

{{ form_start(form_c) }}
    {{ form_label(form_c.FormA.field1) }}
    {{ form_widget(form_c.FormA.field1) }}
    {{ form_errors(form_c.FormA.field1) }}

    {{ form_label(form_c.FormB.field1) }}
    {{ form_widget(form_c.FormB.field1) }}
    {{ form_errors(form_c.FormB.field1) }}
{{ form_end(form_c) }}

If you do a form A submit with value "j@j" you get: enter image description here The error message "This value is not a valid email address" is clearly displayed.

Now if you submit form C with value "j@j" in both fields: enter image description here

The error message "This value is not a valid email address" is not displayed.

Does anyone know why the error messages don't display in case of FormC?

1 Answer 1

2

By default validation won't traverse to properties that are objects or collections. Use the valid constraint:

http://symfony.com/doc/current/reference/constraints/Valid.html

You cans set the traverse option for collections as well.

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

1 Comment

Thanks @Richard! Following your hint lead me to a solution, also I don't find it intuitive at all (had to create an extra EntityForFormC with attributes entityForFormA and entityForFormB in it, and setEntityForFormA-B on it inside the controller). I fell that It would have been more consistant to have {{ form_errors([parentForm].[embeddeForm].[fieldOfEmbeddedFrom]) }} to work when you have {{ form_label([parentForm].[embeddeForm].[fieldOfEmbeddedFrom]) }} and {{ form_widget([parentForm].[embeddeForm].[fieldOfEmbeddedFrom]) }} which do the job that way.

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.