4

我的表格如下所示:

class CpanelRetailerForm extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('name', 'text', array(
                    'attr' => array(
                        'class' => 'text-input',
                        'size' => '50'
                    ),
                    'required' => false
                ))
                ->add('email', 'email', array(
                    'attr' => array(
                        'class' => 'text-input',
                        'size' => '50'
                    ),
                    'required' => false
                ))
                ->add('addUser', 'checkbox', array(
                    'label' => 'Add User account',
                    'required' => false,
                    'mapped' => false
                ))
                ->add('user',new CpanelUserForm());
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\TestBundle\Entity\Retailer',
            //'cascade_validation' => true
        ));
    }

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

我想根据是否选中 addUser 字段从控制器动态设置此行。

cascade_validation' => true

这是我的控制器代码:

$form = $this->createForm(new CpanelRetailerForm(), new Retailer());
    $form->
    if ($this->getRequest()->isMethod('POST')) {
        $form->bind($this->getRequest());
        if ($form->get('addUser')->getData()) {
         // then set the cascade_validation to true here
        }
}

如何在控制器内部执行此操作?

我的尝试:在我的表单类中添加了这一行:

 $builder->addEventListener(
            FormEvents::POST_SUBMIT, function(FormEvent $event) {
                $form = $event->getForm();
                $addUser = $form->get('addUser')->getData();
                $validation = false;
                if ($addUser) {
                    $validation = true;
                }
                $resolver = new OptionsResolver();
                $resolver->setDefaults(array(
                    'cascade_validation' => $validation
                ));
                $this->setDefaultOptions($resolver);
            }
    );

这对我不起作用。虽然我在 $addUser 中收到数据,但没有添加 cascade_validation

4

1 回答 1

0

如何在控制器内部执行此操作?

你不能!这就是简单的答案。让我们看一下以下简单的表单类:

class TestType extends AbstractType {

    /**
     * @var boolean
     */
    private $myOption;

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $this->myOption = false;

        $builder
            ->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) {
                dump('formEvents::PRE_SET_DATA');
            })
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
                dump('FormEvents::POST_SET_DATA');
            })
            ->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
                dump('FormEvents::PRE_SUBMIT');
            })
            ->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
                dump('FormEvents::SUBMIT');
            })
            ->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
                dump('formEvents::POST_SUBMIT');
            })
            ->add('name', TextType::class)
            ->add('send', SubmitType::class);
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setRequired(array(
            'my_option'
        ));
        $resolver->setDefaults(array(
            'my_option' => $this->setMyOption()
        ));
    }

    /**
     * @return bool
     */
    public function setMyOption() {
        dump($this->myOption);
        return $this->myOption;
    }
}

让我们来看看如何在 Controller 中渲染和处理表单:

public function formAction(Request $request) {

    $form = $this->createForm(TestType::class);

    dump('calledCreateForm');

    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()) {
        dump('finished');
        dump($form->getData());
        die();
    }

    return $this->render('@TestPra/Test/test_form.html.twig', array(
        'form'  => $form->createView()
    ));
    
}

提交表单后,您将获得以下输出顺序:

  1. $this->setMyOption()>null
  2. FormEvents::PRE_SET_DATA
  3. FormEvents::POST_SET_DATA
  4. calledCreateForm
  5. FormEvents::PRE_SUBMIT
  6. FormEvents::SUBMIT
  7. FormEvents::POST_SUBMIT
  8. 完成的

allways 被调用的第一件事是configureOptions,因为在调用之前你没有填充表单的任何数据,所以如果handleRequest不操作 Symfonys 表单组件,就无法更改已经创建的表单的选项。

于 2017-09-26T22:35:05.523 回答