How to clear field value with Symfony2 forms

后端 未结 3 2133
日久生厌
日久生厌 2021-02-14 00:56

I\'m writing my own CAPTCHA class and when the form doesn\'t validate, I don\'t want to pre-populate the captcha input with the previous answer, for obvious reasons. I just want

3条回答
  •  灰色年华
    2021-02-14 01:20

    I think you want to handle this form field like Symfony handles a password field: it won't get populated. Let's take a look at the PasswordType:

    namespace Symfony\Component\Form\Extension\Core\Type;
    
    class PasswordType extends AbstractType
    {
        public function buildView(FormView $view, FormInterface $form, array $options)
        {
            if ($options['always_empty'] || !$form->isSubmitted()) {
                $view->vars['value'] = '';
            }
        }
    
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'always_empty' => true,
                'trim' => false,
            ));
        }
    
        //...
    }
    

    So, it's pretty simple: just add $view->vars['value'] = '' in the buildView method of your FormType (i.e. CaptchaType). That means the data of the field is not being cleared, but it won't be passed to the Twig template. Different approach, but the result is the same: the password field stays empty after validation failed.

    If you are really lazy, you can use the PasswordType, but since the input of that field will be masked (*****), will that make an annoying captcha field even worse.

    Your Form Type maybe look like this:

    class CaptchaType extends AbstractType
    {
        /**
         * {@inheritdoc}
         */
        public function buildView(FormView $view, FormInterface $form, array $options)
        {
            $view->vars['value'] = '';
        }
    
        /**
         * {@inheritdoc}
         */
        public function getParent()
        {
            return __NAMESPACE__.'\TextType';
        }
    
        /**
         * {@inheritdoc}
         */
        public function getName()
        {
            return $this->getBlockPrefix();
        }
    
        /**
         * {@inheritdoc}
         */
        public function getBlockPrefix()
        {
            return 'captcha';
        }
    }
    

    Edit: Just found that CaptchaBundle took the same approach.

提交回复
热议问题