How to change form_row behavior in Symfony 2 / Twig

By default, a form_row(form.name) displayed something like:

 <div><label for="form_name" class=" required">Name</label><input type="text" id="form_name" name="form[name]" required="required" maxlength="45" value=""></div> 

How / where can I change the behavior of form_row() to, for example:

 <div class="someClassName"><label for="form_name" class=" required">Name</label></div><div class="someOtherClassName"><input type="text" id="form_name" name="form[name]" required="required" maxlength="45" value=""></div> 
+6
source share
1 answer

You can check this URL: http://symfony.com/doc/2.0/cookbook/form/form_customization.html#cookbook-form-theming-methods

Here is a paragraph about setting form_row ().

Here is a simple example. By default, form_row () will create a simple html structure as follows:

TWIG:

 {{ form_row(form.email, { 'label' : 'Your email address' }) }} 

HTML:

 <div> <label for="register_email" class=" required">Your email address</label> <input type="email" id="register_email" name="register[email]" required="required" /> </div> 

So, according to the docs, you can create a new branch template and add class = "form_row" to the field and label environment. Put it in YourBundle / views / Form / fields.html.twig and put the following code here:

 {% block field_row %} <div class="form_row"> {{ form_label(form) }} {{ form_errors(form) }} {{ form_widget(form) }} </div> {% endblock field_row %} 

In the template file, add the following line:

 {% form_theme form 'YourBundle:Form:fields.html.twig' %} 

Now the form_row template from the file you created will be used and will return the following HTML code:

 <div class="form_row"> <label for="register_email" class=" required">Email</label> <input type="email" id="register_email" name="register[email]" required="required" /> </div> 

Hope this helps.

+17
source

Source: https://habr.com/ru/post/903227/


All Articles