Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony, dynamic form modification won't use Anonymous functions
    primarykey
    data
    text
    <p>Ok, here's the problem, I am using Symfony 2.3.5 on a unix shared hosting account running php v5.3.27. In my Symfony project I am attempting to use an EventListener to dynamically modify a form because I have some options that can't be determined until after the client is chosen. I have been following the example in the Symfony cookbook at <a href="http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html" rel="nofollow">http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html</a> to build the form. I have the following code in my form class:</p> <pre><code>&lt;?php // src/Save/ReportBundle/Form/Type/ReportSettingsType.php namespace Save\ReportBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; class ReportSettingsType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder-&gt;add('client', 'entity', array( 'class'=&gt;'Save\ClientBundle\Entity\Client', 'property'=&gt;'name', 'label'=&gt;'Client: ', 'empty_value' =&gt; '-- Select Client --' )) -&gt;add('newReport', 'choice', array( 'choices' =&gt; array( 1 =&gt; 'Yes', 0 =&gt; 'No' ), 'label' =&gt; 'Run New Report: ', 'mapped' =&gt; false, 'expanded' =&gt; true, 'multiple' =&gt; false )) -&gt;add('reportType', 'choice', array( 'choices' =&gt; array( 'aggregate' =&gt; 'Aggregate', 'filtered' =&gt; 'Filtered' ), 'label' =&gt; 'Report Type: ', 'expanded' =&gt; true, 'multiple' =&gt; false )) -&gt;add('reportSections', 'choice', array( 'choices' =&gt; array( 'welcomeLetter' =&gt; 'Welcome Letter Page', 'reportDetails' =&gt; 'Report Details Page', 'qualitativeAnalysis' =&gt; 'Qualitative Analysis', 'statusSummary' =&gt; 'Status Summary Graphs', 'existingConditions' =&gt; 'Existing Conditions Graphs', 'priorityActions' =&gt; 'Priority Action Plans', 'employeeEngagement' =&gt; 'Employee Engagement Benchmarks', 'futureDiscussions' =&gt; 'Future Discussions' ), 'label' =&gt; 'Report Sections: ', 'multiple' =&gt; true, 'expanded' =&gt; true )) -&gt;add('processReport', 'submit', array( 'label' =&gt; 'Process Report' )); $formModifier = function(FormInterface $form, \Save\ReportBundle\Entity\Report $report){ $filters = $data-&gt;getClient()-&gt;getClientAreas(); $orders = $data-&gt;getClient()-&gt;getOrders(); $form-&gt;add('order', 'entity', array( 'class' =&gt; 'Save\OrderBundle\Entity\Orders', 'property' =&gt; 'selectLabel', 'choices' =&gt; $orders )) -&gt;add('reportFilter', 'entity', array( 'class' =&gt; 'Save\ClientBundle\Entity\ClientArea', 'property' =&gt; 'name', 'choices' =&gt; $filters )); }; $builder-&gt;addEventListener( FormEvents::PRE_SET_DATA, function(FormEvent $event){ $form = $event-&gt;getForm(); $data = $event-&gt;getData(); $formModifier($event-&gt;getForm(), $data); }); $builder-&gt;get('client')-&gt;addEventListener( FormEvents::POST_SUBMIT, function(FormEvent $event) use ($formModifier){ $report = $event-&gt;getForm()-&gt;getData(); $filters = $report-&gt;getClient()-&gt;getClientAreas(); $orders = $report-&gt;getClient()-&gt;getOrders(); $formModifier($event-&gt;getForm()-&gt;getParent(), $report); }); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver-&gt;setDefaults(array( 'data_class' =&gt; 'Save\ReportBundle\Entity\Report' )); } public function getParent() { return 'form'; } public function getName() { return 'reportsettingstype'; } } ?&gt; </code></pre> <p>The problem lies with this line <code>$formModifier($event-&gt;getForm(), $data);</code> When it calls this line I get the following message </p> <pre><code>FatalErrorException: Error: Function name must be a string in [FILE DIRECTORY]/ReportSettingsType.php line 55 </code></pre> <p>I have looked at the PHP documentation on anonymous functions here <a href="http://php.net/manual/en/functions.anonymous.php" rel="nofollow">http://php.net/manual/en/functions.anonymous.php</a> which utilize the PHP Closure class. According to everything I've been able to find so far, this should work the way it is written but I am at my wits end trying to figure this one out.</p> <p>Any constructive help would be greatly appreciated</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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