【问题标题】:How get field value in form builder in Symfony如何在 Symfony 的表单构建器中获取字段值
【发布时间】:2017-04-05 08:02:02
【问题描述】:

如何在 Symfony 的表单构建器中获取字段值。 我在表单中有 2 个下拉菜单 我想在页面打开时根据 Dropdown1 在 Dropdown2 中设置相关选项。

这是我的表格

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'
        ));
    });

【问题讨论】:

    标签: php forms symfony symfony-forms builder


    【解决方案1】:

    您无法从$builder 获取表单数据,因为...它是表单builder,而不是表单。它还不包含任何数据。

    要完成这项工作,您需要使用FormEvents。在这种情况下,您可能需要FormEvents::PRE_SET_DATA 事件监听器。

        $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { 
            $form = $event->getForm();
            // in your case it's C2EducateToolsBundle:LeadSourceSubs
            $entity = $event->getData();
            $leadSource = $entity->getLeadSource();
    
            // adding this field again will override it.
            $form->add('leadSourceSub', 'entity', 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'
                ));
            }
        });
    

    请注意,此代码未经测试,可能需要进行一些验证,例如检查 $entity 是否是您所期望的。

    【讨论】:

    • 是的,我从Dynamic Generation for Submitted Forms尝试了同样的方法。我收到错误Catchable Fatal Error: Argument 1 passed to C2Educate\ToolsBundle\Form\Type\StudentsType::C2Educate\ToolsBundle\Form\Type\{closure}() must be an instance of C2Educate\ToolsBundle\Form\Type\FormEvent, instance of Symfony\Component\Form\Event\DataEvent given in C:\xampp\htdocs\c2Education\src\C2Educate\ToolsBundle\Form\Type\StudentsType.php line 121
    • 您可能只是复制了代码而忘记了导入命名空间。 use Symfony\Component\Form\FormEvent;
    • 看来你的 Symfony 已经很老了,所以可能需要use Symfony\Component\Form\Event\DataEvent 而不是上面的那个,并修改闭包以期望DateEvent 而不是FormEvent
    • 是的。我使用了 DataEvent,它工作正常,当我尝试访问我的实体时它不可用。我在 Field2 中更新了我的问题,你能复习一下吗。
    • 我收到了这个问题Fatal error: Call to a member function getLeadSourceID() on null in C:\xampp\htdocs\c2Education\src\C2Educate\ToolsBundle\Form\Type\StudentsType.php on line 123你能告诉我哪里做错了吗。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多