Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can create action, that will be return json array with employees of company. When the list of companies changed you must request this action by ajax and create select list of employees.</p> <p>Form controller action:</p> <pre class="lang-php prettyprint-override"><code>/** * @Route("/job", name="_demo_job") * @Template() */ public function jobAction() { $form = $this-&gt;createForm(new JobType()); $request = $this-&gt;getRequest(); if ($request-&gt;getMethod() == 'POST') { $form-&gt;bindRequest($request); $data = $form-&gt;getData(); // some operations with form data } return array( 'form' =&gt; $form-&gt;createView(), ); } </code></pre> <p>Conroller, that return employees by company in json:</p> <pre class="lang-php prettyprint-override"><code>/** * Finds all employees by company * * @Route("/{id}/show", name="employees_by_category") */ public function listByCompanyAction($id) { $request = $this-&gt;getRequest(); if ($request-&gt;isXmlHttpRequest() &amp;&amp; $request-&gt;getMethod() == 'POST') { $em = $this-&gt;getDoctrine()-&gt;getEntityManager(); $company = $em-&gt;getRepository('AcmeDemoBundle:Company')-&gt;find($id); // create array for json response $empoloyees = array(); foreach ($company-&gt;getEmployees() as $employee) { $empoloyees[] = array($employee-&gt;getId(), $employee-&gt;getName()); } $response = new Response(json_encode($empoloyees)); $response-&gt;headers-&gt;set('Content-Type', 'application/json'); return $response; } return new Response(); } </code></pre> <p>Form template:</p> <pre class="lang-js prettyprint-override"><code>&lt;script type="text/javascript"&gt; $(document).ready(function() { $('#form_company').change(function() { var companyId = $(this).val(); $.post('{{ route }}/' + companyId + '/show', function(data) { // last selected employee var selectedVal = $('option:selected', '#form_employee').attr('value'); $('#form_employee option').remove(); for (i in data) { // create option with employee var option = $('&lt;option&gt;&lt;/option&gt;'). attr('value', data[i][0]). text(data[i][1]); // set selected employee if (data[i][0] == selectedVal) { option.attr('selected', 'selected'); } // append to employee to employees select $('#form_employee').append(option); } }, 'json'); }) // request employees by company $('#form_company').change(); }) &lt;/script&gt; </code></pre> <pre class="lang-twig prettyprint-override"><code>&lt;form action="{{ path('_demo_job') }}" method="post" {{ form_enctype(form) }}&gt; {{ form_widget(form) }} &lt;input type="submit" /&gt; &lt;/form&gt; </code></pre> <p>For more detail example you should describe your problem in more detail.</p>
 

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