So i am using Symfony 2.5.8 in my project.
I have set up a form and some validation on the entity which is bindet to the form.
Everything works correctly!
Now i am trying to change the form error rendering of twig.
I render the form in the view like this
{{ form(form, {attr: {novalidate: 'novalidate'}}) }}
The errors are now rendered above the specific input field.
How can i change this to render the error messages above the whole form?
Any help is really appreciated.
You can customize your form using twig functions.
For example, for a form attribute named 'age', you can do the following :
{{ form_label(form.age) }}
{{ form_errors(form.age) }}
{{ form_widget(form.age) }}
Here is the symfony doc about form customization.
Related
My Symfony project consists of 3 bundles 2 of which provides forms.
I use twig in my view templates.
The first bundle has a view which includes the forms from the other 2 bundles.
My problem is when I attempt to submit either of the 2 included forms, the other form's required field is highlighted - which interferes with either of the forms submissions and data is not committed.
How I can have multiple forms on the same page which don't interfere with each others submission?
Make sure you are not nesting the forms.
Something like this will be fine:
{{ form_start(myForm) }}
{{ form_widget(myForm) }}
{{ form_end(myForm) }}
{{ form_start(anotherForm) }}
{{ form_widget(anotherForm) }}
{{ form_end(anotherForm) }}
but something like this will not:
{{ form_start(myForm) }}
{{ form_widget(myForm) }}
{{ form_start(anotherForm) }}
{{ form_widget(anotherForm) }}
{{ form_end(anotherForm) }}
{{ form_end(myForm) }}
Im trying to guess why the form I submit is not valid, since my Entity has no constraints.
I pass to the twig template 'error' => $form->getErrors() but then I have no idea how to show in the twig the error message
Thanks for all
From the docs:
{{ form_errors(form.fooProperrty) }}
And
{{ form_errors(form) }}
If I may guess, your form is not validating because you forgot to render the csrf token, which you can render by doing
{{ form_rest(form) }}
When I want to add some attributes to symfony2 form elements I can just use the "attr" attribute. But how can I give some id / class / style / other to the starting form tag itself?
{{ form_widget(form.name, { 'attr': {'class': 'foo'} }) }}
This is the correct syntax for a form widget with the class foo. In other words a form field in your form.
You can apply the same for the <form> tag.
This is done by rendering
{{ form_start(form, { 'attr': {'class': 'foo', 'id': 'bar', ... } }) }}
See this short documentation on form_start here
Renders the start tag of a form. This helper takes care of printing the configured method and target action of the form. It will also include the correct enctype property if the form contains upload fields.
You might also be interested in form variables. They are documented here. All in all there is a whole documentation on all functions and variables you can use here
I wouldn't recommend you to use inline styles. Just do the styling with the ID and/or class you gave the form.
I have a form widget with several choices (many-to-many relationship)
in twig template I can iterate over the checkboxes:
{% for choice in form.downloads %}
{{ form_widget(choice) }} {{ form_label(choice) }}<br />
{% endfor %}
I'd like to acces the choices directly (they should bi formatted end positioned differently)
I tried several syntaxes but the doesn't work
{{ form_widget(form.downloads.0) }} {{ form_label(form.downloads.0) }}<br />
{{ form_widget(form.downloads['0']) }} {{ form_label(form.downloads['0']) }}<br />
{{ form_widget(form.downloads[0]) }} {{ form_label(form.downloads[0]) }}<br />
Do I use the wrong array keys or is array access generally not possible?
Array Access is possible when you're using Twig.
I guess the error you got when you're trying to access the first generated checkbox using
{{ form_widget(form.downloads.0) }} {{ form_label(form.downloads.0) }}<br />
is
Method "0" for object "Symfony\Component\Form\FormView" does not exist in ...
So, you've just to use the child name of your checkbox. You should have in your buildForm something like:
$builder->add('childName', 'anyTypeYouWant', array())
But I guess you're using collection type to generate your checkboxes. In this specific case
{{ form_widget(form.downloads.0) }} {{ form_label(form.downloads.0) }}<br />
should work fine! I already used it to access specific collection fields without customized keys.
You should also use the twig debug extension to check your form.downloads
{% debug form.downloads %}
and if the debug doesn't work, you've to add in your "app/config/config.yml" file
services:
debug.twig.extension:
class: Twig_Extensions_Extension_Debug
tags: [{ name: 'twig.extension' }]
For rendering form errors in a twig template, you just have to use the form_errors twig macro without difference if it is a global form error or a field error.
But in my case, a global error is not rendered like a field error, so I can't use the form_errors twig macro for the two cases. I decide to use the macro for the field error & I would like to get the global form errors from the Symfony\Component\Form\FormView object. The goal is to iterate the global errors in the twig template & render them like I want.
Actually, I don't find any ressources on the symfony2 documentation which can help me.
Finally, I found the solution by myself. For the people who want to do the same thing, the solution is to call $formView->get("errors") which gives you an array of FormError
I'm using symfony 2.5 and it worked perfect for me in this way.
MyController
$error = new FormError(ErrorMessages::USER_NOT_AUTHENTICATED);
$form->addError($error);
MyView
{% for error in form.vars.errors %}
<div class="alert alert-danger" role="alert">
{{ error.messageTemplate|trans(error.messageParameters, 'validators')~'' }}
</div>
{% endfor %}
hope this will save someones time.
in symfony 2.3 all accessor methods have been removed in favor of public properties to increase performance.
$formView->get("errors");
is now:
$formView->vars["errors"];
Visit UPGRADE-2.1.md and refer to section "Deprecations" for more information.