如何在另一个选择框中使用选择框相关? [英] How to use select box related on another select box?

查看:96
本文介绍了如何在另一个选择框中使用选择框相关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Symfony中使用相关的选择框?

How to use related select boxes in Symfony ?

假设我有一个包含compagnies的选择列表,另一个包含所选公司的员工。如何在Symfony中定义这些?
我已经创建了所有与JavaScript相关的代码,但是在提交表单并在某些字段上发生错误时,所有子选择字段将重置为null。

Let's say, I have a select list containing compagnies and another containing employees of the selected company. How do I define those in Symfony? I have already created all the Javascript related code but when submitting my form and having errors on some fields, the all "sub" select fields are reset to null.

任何想法?

谢谢,

Any ideas?
Thanks,

编辑:由于问题似乎是误会,我会添加一些细节:

EDIT : As the question seems to be misunderstood, I'll add some precisions :


  1. 我有一个实体公司,其中包含使用@OneToMany关系的员工名单。

  2. 当我在选择/下拉列表中选择公司时,包含员工的第二个下拉列表将通过jQuery进行更新。 该部分已完成,完全正常工作

  3. 当表单无错误提交时,实体表单解决方案可以正常工作。

  4. 提交包含错误的表单时,第二个下拉列表包含所有可能的值。它们不会在所选公司上过滤。

  1. I have an entity Company containing a list of Employees using a @OneToMany relation.
  2. When I select a company in a select/dropdown list, the second dropdown list containing the employees is updated via jQuery. That part is done, works perfectly
  3. When submitting the form without errors, the entity form solution works fine.
  4. When submitting the form containing errors, the second dropdown list is containing all possible values. They are not filtered on the selected company.



试用解决方案:




  • 想法是使用表单实体类型,认为组件可能会在另一个字段上被绑定。即。根据所选公司的价值更新员工名单。

  • Tried solutions :

    • My first idea was to use the form entity type, thinking the component could be binded somehow on another field. ie. update list of employees based on the value of the selected company.
    • 不工作,没有办法开箱即可。即使不是开箱即用的解决方案...


      • 然后我想到了手动通过选择公司作为参数到第二个下拉列表的查询构建器。

      • Then I thought about manually passing the selected company as parameter to the query builder of the second dropdown list.

      但是,当表单创建时,值为空。值仅在 bindRequest 上设置。

      But when the form is created, the values are empty. The values are only set on bindRequest.


      • 想想使用选择类型。通过Javascript将所有过滤功能委托给UI。意义当页面加载时,会出现一个空列表,并根据所选公司填写Javascript。

      ,但是我觉得在这里真的很难编程,没有别的话。

      问题已在这里被问及Symfony2邮件列表,Twitter和官方Symfony 2论坛。当然,在发布我的问题之前,我已经搜索过几次。

      Question has been asked here, on the Symfony2 mailing-list, on Twitter and the officiel Symfony 2 forum. I have of course searched each of those several times before posting my questions.

      推荐答案

      关于你已经尝试过的内容,我想你应该重试你的第一个/第二个想法:

      Concerning what you already tried, I think you should retry your first/second ideas:


      我的第一个想法是使用表单实体类型,认为组件
      可能会在另一个字段上绑定。即。根据所选公司的价值更新员工
      的列表。

      My first idea was to use the form entity type, thinking the component could be binded somehow on another field. ie. update list of employees based on the value of the selected company.

      您可以使用 entity type。
      所有你需要做的是定义好的选项:

      You can populate the employees select box using the entity type. All you have to do is to define the good options:

      class FooType extends AbstractType
      {
          public function buildForm(FormBuilder $builder, array $options)
          {
              $builder
                  ->add('employee', 'entity', array(
                      'class' => 'Entity\Employee',
                      'query_builder' => function ($repository) use($options) {
                          return $repository
                              ->createQueryBuilder('e')
                              ->where('e.company = :company')
                              ->setParameter('company', $options['companyId'])
                          ;
                      },
                  ))
              ;
          }
      
          public function getDefaultOptions(array $options)
          {
              return array('data_class' => 'Entity\Foo', 'companyId' => null);
          }
      }
      




      然后我想到手动将选定的公司作为
      参数传递给第二个下拉列表的查询构建器。

      Then I thought about manually passing the selected company as parameter to the query builder of the second dropdown list.

      这里的示例过滤员工基于companyId表单的选项列表。
      您可以通过直接过滤表单数据中的公司来修改此行为。

      The example here filters the employees list based on the companyId form's option. You can modify this behavior by filtering directly on the company present in the form's data.

      public function buildForm(FormBuilder $builder, array $options)
      {
          $companyId = $builder->getData()->getCompanyId();
          $builder
              ->add('employee', 'entity', array(
                  'class' => 'Entity\Employee',
                  'query_builder' => function ($repository) use ($companyId) {
                      return $repository
                          ->createQueryBuilder('e')
                          ->where('e.company = :company')
                          ->setParameter('company', $companyId)
                      ;
                  },
              ))
          ;
      }
      

      您还必须实现 getEmployee() / code>和 setEmployee() Entity\Foo 类中的方法。

      You still have to implement the getEmployee() and setEmployee() methods in your Entity\Foo class.


      但是,当表单创建时,值为空。值仅在
      bindRequest上设置。

      But when the form is created, the values are empty. The values are only set on bindRequest.

      否。当您使用form factory(第三个参数)创建您的表单时,设置值,
      当您调用 $ form-> setData($ foo); 时,OR。数据被修改,当你绑定
      新的输入到表单。

      No. Values are set when you create you form using form factory (third argument), OR when you call $form->setData($foo);. Data is modified when you bind new inputs to the form.

      这可能是一个问题方法:由于您更改了公司(因此更改了员工),因此可能在表单选择列表中绑定表单的员工ID不可用。

      这篇关于如何在另一个选择框中使用选择框相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆