动态加载下拉列表

时间:2013-07-19 07:37:11

标签: symfony

我一直在搜索这个问题很长一段时间了。好吧,我动态地将表单加载到symfony2中的twig。这是下面的代码:

class NewsType extends AbstractType
{   
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $formFactory = $builder->getFormFactory();

        $builder
            ->add('title')
            ->add('body')
            ->add('userType', 'choice', array(
            'choices' => array (
              'CT' => 'Contract',
              'PT' => 'Permanent',),))
            ->add('Save', 'submit')   
                 ->getForm()
        ;

        $builder->addEventListener(
        FormEvents::POST_BIND,
        function (FormEvent $event) use ($formFactory) {


            var_dump($event);

            $choices = array("key1" => "value1", "key2" => "value2");

            $event->getForm()->add(
              $formFactory->createNamed("my_dynamic_field_name", "choice", null, array(
                        "empty_value" => "Choose an option",
                        "auto_initialize" => false,
                        "label" => "My dynamic field: ",
                        "choices" => $choices
                    )
                )
            );


        }
        );
  }

    public function getName()
    {
        return 'remove_form_field';
    }

    // ...
}

我想要实现的是,当选择'userType'下拉值时,事件应该触发并加载'my_dynamic_field_name'下拉列表。我怎么能实现这个目标? (这不使用“提交”按钮,当用户从下拉列表中选择一个值时,另一个下拉列表应填充)

0 个答案:

没有答案