好的,这是问题,我在运行php v5.3.27的unix共享主机帐户上使用Symfony 2.3.5。在我的Symfony项目中,我试图使用EventListener动态修改表单,因为我有一些选项在选择客户端之前无法确定。我一直在http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html的Symfony食谱中关注这个例子来构建表格。我在表单类中有以下代码:
<?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->add('client', 'entity', array(
'class'=>'Save\ClientBundle\Entity\Client',
'property'=>'name',
'label'=>'Client: ',
'empty_value' => '-- Select Client --'
))
->add('newReport', 'choice', array(
'choices' => array(
1 => 'Yes',
0 => 'No'
),
'label' => 'Run New Report: ',
'mapped' => false,
'expanded' => true,
'multiple' => false
))
->add('reportType', 'choice', array(
'choices' => array(
'aggregate' => 'Aggregate',
'filtered' => 'Filtered'
),
'label' => 'Report Type: ',
'expanded' => true,
'multiple' => false
))
->add('reportSections', 'choice', array(
'choices' => array(
'welcomeLetter' => 'Welcome Letter Page',
'reportDetails' => 'Report Details Page',
'qualitativeAnalysis' => 'Qualitative Analysis',
'statusSummary' => 'Status Summary Graphs',
'existingConditions' => 'Existing Conditions Graphs',
'priorityActions' => 'Priority Action Plans',
'employeeEngagement' => 'Employee Engagement Benchmarks',
'futureDiscussions' => 'Future Discussions'
),
'label' => 'Report Sections: ',
'multiple' => true,
'expanded' => true
))
->add('processReport', 'submit', array(
'label' => 'Process Report'
));
$formModifier = function(FormInterface $form, \Save\ReportBundle\Entity\Report $report){
$filters = $data->getClient()->getClientAreas();
$orders = $data->getClient()->getOrders();
$form->add('order', 'entity', array(
'class' => 'Save\OrderBundle\Entity\Orders',
'property' => 'selectLabel',
'choices' => $orders
))
->add('reportFilter', 'entity', array(
'class' => 'Save\ClientBundle\Entity\ClientArea',
'property' => 'name',
'choices' => $filters
));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function(FormEvent $event){
$form = $event->getForm();
$data = $event->getData();
$formModifier($event->getForm(), $data);
});
$builder->get('client')->addEventListener(
FormEvents::POST_SUBMIT,
function(FormEvent $event) use ($formModifier){
$report = $event->getForm()->getData();
$filters = $report->getClient()->getClientAreas();
$orders = $report->getClient()->getOrders();
$formModifier($event->getForm()->getParent(), $report);
});
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Save\ReportBundle\Entity\Report'
));
}
public function getParent()
{
return 'form';
}
public function getName()
{
return 'reportsettingstype';
}
}
?>
问题出在这一行$formModifier($event->getForm(), $data);
当它调用此行时,我收到以下消息
FatalErrorException: Error: Function name must be a string in [FILE DIRECTORY]/ReportSettingsType.php line 55
我已经查看了使用PHP Closure类的匿名函数http://php.net/manual/en/functions.anonymous.php的PHP文档。根据我迄今为止所能找到的所有内容,这应该按照它的编写方式工作,但我最终想要弄清楚这一点。
非常感谢任何建设性的帮助
答案 0 :(得分:2)
它缺少闭包的use
关键字,要在变量$ formModifier中关闭。由于目前在匿名函数中没有任何名为$formModifier
的变量,PHP抱怨。
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function(FormEvent $event){ // use ($formModifier)
$form = $event->getForm();
$data = $event->getData();
$formModifier($event->getForm(), $data);
});