如何使用Symfony2表单清除字段值 [英] How to clear field value with Symfony2 forms

查看:64
本文介绍了如何使用Symfony2表单清除字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写自己的CAPTCHA类,并且当表单不通过验证时,出于明显的原因,我不想使用先前的答案来预填充验证码输入.我只想在呈现输入之前清除输入.

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 to the clear the input before it's rendered.

我发现 data 选项仅适用于默认值,该默认值会被用户输入的内容覆盖.我尝试了以下代码:

I've discovered the data option is only for the default value, which is overwritten by what the user enters. I tried the following code:

$form->get('captcha')->setData(null);

..将请求与表单绑定之后,但是会抛出AlreadyBoundException.实际上,我已经设法使其与之合作:

.. After the request is bound with the form, but an AlreadyBoundException is thrown. I have actually managed to get it working with:

if (isset($formView->children['captcha'])) {
    $formView->children['captcha']->vars['value'] = null;
}

但这看起来很不对劲,而且绝对不符合Symfony的标准.在构建表单时,我已经浏览了您可以提供的其他选项,但是看不到任何注意事项.

But that just looks wrong, and definitely not up to Symfony standards. I've looked through the other options you can provide when building the form, but I can't see anything of note.

有人有什么主意吗?

顺便说一句,我一半希望Symfony2附带CAPTCHA解决方案,这主要是我习惯该框架时的学习练习.

By the way, I half expect Symfony2 comes packaged with a CAPTCHA solution, this is mainly a learning exercise while I get used to the framework.

推荐答案

我认为您想像Symfony处理password字段那样处理此表单字段:不会填充它.让我们看一下 PasswordType :

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,
        ));
    }

    //...
}

所以,这非常简单:只需在FormType(即CaptchaType)的buildView方法中添加$view->vars['value'] = ''.这意味着该字段的数据不会被清除,但不会传递给Twig模板.方法不同,但结果相同:验证失败后密码字段保持为空.

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.

如果您真的很懒,可以使用PasswordType,但是由于该字段的输入将被屏蔽(*****),因此会使烦人的验证码字段变得更糟.

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';
    }
}

刚刚发现CaptchaBundle采用了相同的方法.

Just found that CaptchaBundle took the same approach.

这篇关于如何使用Symfony2表单清除字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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