好的,问题来了,我在运行 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 文档http://php.net/manual/en/functions.anonymous.php,它利用了 PHP Closure 类。根据到目前为止我所能找到的一切,这应该按照它的编写方式工作,但我正在努力解决这个问题。
任何建设性的帮助将不胜感激