根据多个约束验证表单字段

发布于 2024-12-26 10:14:34 字数 817 浏览 0 评论 0原文

我已经构建了一个注册表单,我想验证其中的字段。 在我的 RegistrationFormType 中,我有以下代码:

public function getDefaultOptions(array $options)
    {
        $collectionConstraint = new Collection(array(
            'email' => new Collection(array(
                new NotBlank(),
                new Email(array('message' => 'Invalid email addressadsfa')),
                )),
            'username' => new Email(array('message' => 'arg Invalid email addressadsfa')),
            'code' => new MaxLength(array('limit'=>20)),
            'plainPassword' => new MaxLength(array('limit'=>20)),
        ));

        return array(
            'csrf_protection' => false,
            'validation_constraint' => $collectionConstraint,
        );
    }

问题是:电子邮件验证不起作用。我做错了什么?

I have built a registration form where I want to validate fields in it.
In my RegistrationFormType I have following code:

public function getDefaultOptions(array $options)
    {
        $collectionConstraint = new Collection(array(
            'email' => new Collection(array(
                new NotBlank(),
                new Email(array('message' => 'Invalid email addressadsfa')),
                )),
            'username' => new Email(array('message' => 'arg Invalid email addressadsfa')),
            'code' => new MaxLength(array('limit'=>20)),
            'plainPassword' => new MaxLength(array('limit'=>20)),
        ));

        return array(
            'csrf_protection' => false,
            'validation_constraint' => $collectionConstraint,
        );
    }

Problem is: The email validation does not work. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2025-01-02 10:14:34

您不需要将电子邮件条目设为集合,只需使用简单的数组即可。所以:

public function getDefaultOptions(array $options)
{
    $collectionConstraint = new Collection(array(
        'email' => array(
            new NotBlank(),
            new Email(array('message' => 'Invalid email addressadsfa')),
        ),
        'username' => new Email(array('message' => 'arg Invalid email addressadsfa')),
        'code' => new MaxLength(array('limit'=>20)),
        'plainPassword' => new MaxLength(array('limit'=>20)),
    ));

    return array(
        'csrf_protection' => false,
        'validation_constraint' => $collectionConstraint,
    );
}

You don't need to make the email entry a Collection, just use a simple array. So:

public function getDefaultOptions(array $options)
{
    $collectionConstraint = new Collection(array(
        'email' => array(
            new NotBlank(),
            new Email(array('message' => 'Invalid email addressadsfa')),
        ),
        'username' => new Email(array('message' => 'arg Invalid email addressadsfa')),
        'code' => new MaxLength(array('limit'=>20)),
        'plainPassword' => new MaxLength(array('limit'=>20)),
    ));

    return array(
        'csrf_protection' => false,
        'validation_constraint' => $collectionConstraint,
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文