1
如何在Symfony中的表单构建器中获取字段值。 我有2个下拉框的形式 我想在Dropdown2的时候在页面打开的时候根据Dropdown1来选择相关的选项。如何在Symfony中的表单构建器中获取字段值
这里是我的形式
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\DataEvent;
use C2Educate\ToolsBundle\Entity\Students;
public function buildForm(FormBuilder $builder, array $options) {
字段1:
$builder->add('leadSource', 'entity', array(
'label' => 'How did you hear about C2? Source ',
'class' => 'C2EducateToolsBundle:LeadSources',
'query_builder' => function($repo) {
return $repo->createQueryBuilder('p')->orderBy('p.sort_order', 'ASC');
},
'property' => 'name',
'empty_value' => 'Select'
));
$leadSource = 1;
$leadSource = 1;
- 当我静态分配值它的工作原理,但我想要得到的“leadSource”的值,并将其分配给$leadSource
我想获得leadSource并将其传递给leadSourceSub查询
字段2:
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (DataEvent $event) {
$form = $event->getForm();
$entity = $event->getData();
$leadSource = $entity->getLeadSourceID();
$form->add('leadSourceSub', 'C2Educate\ToolsBundle\Entity\Students', array(
'label' => ' Source Detail ',
'required' => true,
'class' => 'C2EducateToolsBundle:LeadSourceSubs',
'query_builder' => function($repo) use ($leadSource) {
return $repo->createQueryBuilder('p')
->where('p.lead_source_id =:leadSource')
->setParameter('leadSource', $leadSource)
->orderBy('p.sort_order', 'ASC');
},
'property' => 'name',
'empty_value' => 'Select'
));
});
是我试图从相同的[动态生成的表单提交(https://symfony.com/doc/current/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms)。我正在一个Error'Catchable致命错误:传递给C2Educate \ ToolsBundle \表格\类型参数1 \ StudentsType :: C2Educate \ ToolsBundle \表格\型号\ {闭合}()必须C2Educate \ ToolsBundle \表格\型号\ FormEvent,实例的一个实例Symfony的\分量\表格\事件\ DataEvent给出在C:\ XAMPP \ htdocs中\ c2Education的\ src \ C2Educate \ ToolsBundle \表格\型号\ StudentsType.php线121' – Developer
你可能只是复制代码,忘了导入命名空间。 '使用的Symfony \分量\表格\ FormEvent;' –
看来你很老的Symfony,所以可能需要使用'的Symfony \分量\表格\事件\ DataEvent',而不是一个在上面,修改封闭期待'DateEvent'代替'FormEvent'。 –