Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have an application where referees can sign up for different tournaments. For each tournament I need to ask a different set of questions. So I moved the questions into a yaml file and added enough information such that I could create the form elements dynamically. So basically I would have:</p> <pre><code># tourn.yml tourns: opencup: desc: "USSF Open Cup 2013 - Decatur, Alabama - April 12, 13, 14" plan: lodgingNights: Fri: type: radio label: Lodging Friday Night date: '2013-04-12' choices: [No: No, Yes: Yes] default: No Sat: type: radio label: Lodging Saturday Night date: '2013-04-13' choices: [No: No, Yes: Yes] default: No availabilityDays: Fri: type: select label: Availability Friday date: '2013-04-12' default: None choices: None: None Evening: Kickoff 5PM Not Sure: Not Sure etc </code></pre> <p>So the plan array ends up being passed to the plan sub form which in turn creates all the individual form elements. I then have a single plan entity which just stores all the individual selections in an array database column. Seems to work okay.</p> <p>=======================================================================</p> <p>Comment asked for some more code. Here is my "dynamic" PlanFormType"</p> <pre><code>class PlanFormType extends AbstractType { protected $metaData = null; public function getName() { return 'cerad_tourn_person_plan'; } public function setMetaData($data) { $this-&gt;metaData = $data; } public function getMetaData( ) { return $this-&gt;metaData; } public function buildForm(FormBuilderInterface $builder, array $options) { $items = $this-&gt;getMetaData(); foreach($items as $name =&gt; $item) { $isChoice = false; switch($item['type']) { case 'radio': $isChoice = true; $expanded = true; $multiple = false; $attr = array('class' =&gt; 'radio-medium'); break; case 'select': $isChoice = true; $expanded = false; $multiple = false; $attr = array(); break; case 'text': $attr = array(); if (isset($item['size'])) $attr['size'] = $item['size']; $builder-&gt;add($name,'text',array( 'label' =&gt; $item['label'], 'required' =&gt; false, 'attr' =&gt; $attr, )); break; } if ($isChoice) { $builder-&gt;add($name,'choice',array( 'label' =&gt; $item['label'], 'required' =&gt; false, 'empty_value' =&gt; false, 'choices' =&gt; $item['choices'], 'expanded' =&gt; $expanded, 'multiple' =&gt; $multiple, 'attr' =&gt; $attr, )); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload