【问题标题】:Symfony2 - form global validation not working if validation group is definedSymfony2 - 如果定义了验证组,则表单全局验证不起作用
【发布时间】:2013-12-11 17:20:18
【问题描述】:

我发现,如果我没有登录,我对 textarea 的验证不起作用。如果是我,它工作正常。 这是我的 Comment.php 实体:

 /**
 * @ORM\Column(type="text")
 * @Assert\NotBlank(
 *      message = "Message cannot be blank" 
 * )     
 * @Assert\Length(
 *      min = "3",
 *      minMessage = "Message must have 3 or more characters"           
 * )     
 */
private $content;

在我的 CommentType.php

// ... namespace and uses
class CommentType extends AbstractType
{
    private $user;

    public function __construct($user) {
        $this->user = $user;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\BlogBundle\Entity\Comment',
            'csrf_protection' => true,
            'validation_groups' => (is_null($this->user) ? 'not_logged' : 'Default'), // here I set validation group if user is logged or not
        ));
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->setAction($builder->getAction().'#submit-comment');

        if(is_null($this->user)) {
            $builder->add('author', 'text', array('label' => 'Autor'))
                    ->add('email', 'text', array('label' => 'E-mail (will not show)'))
                    ->add('content', 'textarea', array('label' => 'Text',))
                    ->add('captcha', 'captcha', array('invalid_message' => 'Bad captcha', 'background_color' => array(255, 255, 255) )); 
        }        
        else {
            $builder->add('content', 'textarea', array('label' => 'Text',));
        }
            $builder->add('save', 'submit', array('label' => 'submit'));
    }

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

正如我所写,如果我已登录,它可以正常工作(内容字段),但如果没有,所有字段都经过验证除了内容字段。

有什么想法吗?

【问题讨论】:

    标签: php forms validation symfony


    【解决方案1】:

    如果用户未登录,则您使用的是“not_logged”验证组:

    $resolver->setDefaults(array(
        'data_class' => 'Acme\BlogBundle\Entity\Comment',
        'csrf_protection' => true,
        'validation_groups' => (is_null($this->user) ? 'not_logged' : 'Default'), // here I set validation group if user is logged or not
    ));
    

    但在您的实体中,没有关于该组的信息:

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank(
     *      message = "Message cannot be blank",
     *      groups={"not_logged"} // specified group
     * )
     * @Assert\Length(
     *      min = "3",
     *      minMessage = "Message must have 3 or more characters",
     *      groups={"not_logged"}, // specified group
     * )
     */
    private $content;
    

    【讨论】:

    • 是的,因为我对已登录和未登录的用户使用此实体验证。你没看懂我写的。只有作者,邮件需要验证组 not_logged 因为登录的用户不会看到这个字段。所以这两种用户(登录或未登录)都会有内容 textarea 字段
    • @apo 'Default' 也应该包括在内:groups={"Default", "not_logged"} ? :-)
    • @Martin Lie “默认”组无法附加到组序列。检查它在symfony.com/doc/current/book/…
    • @apo 但是我们这里没有使用组 sequences 吗?
    • 我尝试使用 groups = {"not_logged", "Default"} 但同样的问题。
    【解决方案2】:

    所以我找到了解决方案。我的问题在这里:

         'validation_groups' => (is_null($this->user) ? 'not_logged' : 'Default'), // here I set validation group if user is logged or not
    

    问题是这些组不在数组中,并且 not_logged 组还需要 Default 组(这就是它不起作用的原因)。所以可行的解决方案是:

         'validation_groups' => (is_null($this->user) ? array('Default', 'not_logged') : array('Default')),
    

    感谢您的帮助,很抱歉在这个错误上浪费了时间。

    【讨论】:

      猜你喜欢
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 2022-01-25
      相关资源
      最近更新 更多