php - Wrap Symfony form fields in a div in Twig -


given simple form build:

$form = add('a')       ->add('b')       ->add('c')       ->add('d')       ->add('e')       ->add('f'); 

i'd wrap div tag around abc , def, so:

<div class="section1">     <input type="text" name="a" />     <input type="text" name="b" />     <input type="text" name="c" /> </div> <div class="section2">     <input type="text" name="d" />     <input type="text" name="e" />     <input type="text" name="f" /> </div> 

problem is, able use symfony form component project. there way twig render form fields in groups above? need specify logic says simliar "start section 1 'a' , section 2 'd'", way if fields change inbetween (say remove field name 'b') form still work.

my twig file looks this, not correct:

<form action="#" method="post" {{ form_enctype(form) }}>     {% child in form.children %}         <div class="form_row">              {{ form_label(form) }}             {{ form_errors(form) }}             {{ form_widget(form) }}         </div>     {% endfor %} </form>  

the best way think building form outside controller (as class, extending abstractype)

so, need these files:

  • your controller (as defaultcontroller.php)
  • your twig template (as index.html.twig)
  • your main form (myformtype.php in example)
  • one form each section (in example section1type.php , section2type.php)

so, idea building 1 single form (myformtype) made many individual section forms (section1type , section2type). calling main form in controller , rendering in template (with "for" loop each section).

so here have code:

your controller:

<?php # /src/acme/defaultbundle/controller/defaultcontroller.php  namespace acme\defaultbundle\controller; use symfony\bundle\frameworkbundle\controller\controller; use acme\defaultbundle\form\myformtype;  class defaultcontroller extends controller {     public function indexaction()     {         $form = $this->createform(new myformtype());          return $this->render('acmedefaultbundle:default:index.html.twig', array(             'form' => $form->createview()         ));     } } 

your template:

{# /src/acme/defaultbundle/resources/views/default/index.html.twig #}  {{ form_start(form) }}     <div class="section1">         {% input in form.section1 %}             {{ form_label(input) }}             {{ form_widget(input) }}             <br>         {% endfor %}     </div>     <div class="section2">         {% input in form.section2 %}             {{ form_label(input) }}             {{ form_widget(input) }}             <br>         {% endfor %}     </div> {{ form_end(form) }} 

the main form:

<?php # /src/acme/defaultbundle/form/myformtype.php  namespace acme\defaultbundle\form;  use acme\defaultbundle\form\section1type; use acme\defaultbundle\form\section2type;  use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface;  class myformtype extends abstracttype {     public function buildform(formbuilderinterface $builder, array $options)     {         // calls section forms many need         $builder->add('section1', new section1type());         $builder->add('section2', new section2type());          $builder->add('send', 'submit');     }      public function getname()     {         return 'myform';     } } 

the section1 form:

<?php # /src/acme/defaultbundle/form/section1type.php namespace acme\defaultbundle\form;  use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface;  class section1type extends abstracttype {     public function buildform(formbuilderinterface $builder, array $options)     {         $builder->add('a', 'text');         $builder->add('b', 'text');         $builder->add('c', 'text');     }      public function getname()     {         return 'section1';     } } 

and section2 form:

<?php # /src/acme/defaultbundle/form/section2type.php namespace acme\defaultbundle\form;  use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface;  class section2type extends abstracttype {     public function buildform(formbuilderinterface $builder, array $options)     {         $builder->add('d', 'text');         $builder->add('e', 'text');         $builder->add('f', 'text');     }      public function getname()     {         return 'section2';     } } 

and that's it, can edit each individual section (adding , removing inputs) , of them in template without modifying it.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -