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

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

问题描述

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

比方说,我有一个包含公司的选择列表,另一个包含所选公司的员工.我如何定义 Symfony 中的那些?我已经创建了所有与 Javascript 相关的代码,但是当提交我的表单并在某些字段上出现错误时,所有子"选择字段都被重置为空.

有什么想法吗?
谢谢,

由于问题似乎被误解,我将添加一些精确度:

说明:

  1. 我有一个实体 Company,其中包含使用 @OneToMany 关系的员工列表.
  2. 当我在选择/下拉列表中选择一家公司时,包含员工的第二个下拉列表将通过 jQuery 更新.那部分已经完成,完美
  3. 提交表单没有错误时,实体表单解决方案工作正常.
  4. 提交包含错误的表单时,第二个下拉列表包含所有可能的值.它们不会在所选公司上过滤.

尝试过的解决方案:

  • 我的第一个想法是使用 form entity 类型,认为组件可以以某种方式绑定到另一个字段上.IE.根据所选公司的价值更新员工列表.

不工作,没有开箱即用的方法来做到这一点.即使是非开箱即用的解决方案...

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

但是当创建表单时,值是空的.这些值仅在 bindRequest 上设置.

  • 考虑使用选择类型.通过 Javascript 将所有过滤器功能委托给 UI.这意味着当页面加载时,会出现一个空列表,并根据所选公司由 Javascript 填充.

这确实有效,但我认为除了真正真正丑陋的编程之外别无他法.

附注:

问题已在此处、Symfony2 邮件列表、Twitter 和官方 Symfony 2 论坛上提出.在发布我的问题之前,我当然已经搜索了几次.

解决方案

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

<块引用>

我的第一个想法是使用表单实体类型,认为组件可以以某种方式绑定到另一个领域.IE.更新员工名单基于所选公司的价值.

您可以使用 entity 类型填充员工选择框.您所要做的就是定义好的选项:

class FooType 扩展 AbstractType{公共函数 buildForm(FormBuilder $builder, array $options){$builder->add('员工', '实体', 数组('类' =>'实体员工','query_builder' =>函数($repository)使用($options){返回 $repository->createQueryBuilder('e')->where('e.company = :company')->setParameter('company', $options['companyId']);},));}公共函数 getDefaultOptions(array $options){return array('data_class' => 'EntityFoo', 'companyId' => null);}}

<块引用>

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

此处的示例根据 companyId 表单的选项过滤员工列表.您可以通过直接过滤表单数据中的公司来修改此行为.

公共函数 buildForm(FormBuilder $builder, array $options){$companyId = $builder->getData()->getCompanyId();$builder->add('员工', '实体', 数组('类' =>'实体员工','query_builder' =>函数($repository)使用($companyId){返回 $repository->createQueryBuilder('e')->where('e.company = :company')->setParameter('company', $companyId);},));}

您仍然需要在 EntityFoo 类中实现 getEmployee()setEmployee() 方法.

<块引用>

但是当表单被创建时,值是空的.这些值仅设置在绑定请求.

没有.使用表单工厂(第三个参数)创建表单时设置值,或者当您调用 $form->setData($foo); 时.当您绑定新输入到表单时,数据会被修改.

这种方法可能存在问题:绑定到表单的员工 ID 可能在表单选择列表中不可用,因为您更改了公司(以及员工).

How to use related select boxes in Symfony ?

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 :

Description :

  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.

Not working, there is no way out of the box to do this. Even non out of the box solutions...

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

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

  • Thought about using the choice type. Delegating all filter functionnality to the UI via Javascript. Meaning when the page loads, an empty list appears and is populated by Javascript based on the selected company.

This actually works, but I think there is no other word than really really ugly programming here.

PS :

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.

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' => 'EntityEmployee',
                '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' => 'EntityFoo', 'companyId' => null);
    }
}

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

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' => 'EntityEmployee',
            'query_builder' => function ($repository) use ($companyId) {
                return $repository
                    ->createQueryBuilder('e')
                    ->where('e.company = :company')
                    ->setParameter('company', $companyId)
                ;
            },
        ))
    ;
}

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

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

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.

There could be a problem with this approach: it's possible that the employee id bound to the form is not available in the form choice list, because you changed the company (and thus the employees).

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

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