Symfony 2独特的验证器 [英] Symfony 2 unique validator

查看:63
本文介绍了Symfony 2独特的验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用某些必须唯一的字段(用户名和电子邮件地址)来验证表单.如果我提交表单,则会收到数据库错误.我想像其他所有事情一样使用验证器-现在,我正在尝试在对象中使用自定义getter和isvalidUsername函数,但不确定在对象中使用实体管理器是否是实现此目的的最佳方法.到目前为止,这是我正在使用的东西...

I'm trying to validate a form with some fields that need to be unique - username and email address. If I submit the form I get a database error. I want to use a validator like I did for everything else - right now I'm trying to use custom getters and isvalidUsername functions in the object and I'm not sure if using the entity manager in the object is the best way to do this. Heres what I'm working with so far...

    Frontend\UserBundle\Entity\User:
         properties:
             email:
                  - NotBlank: ~
                  - Email: ~
         username:
             - NotBlank: ~
         getters:
              validUsername:
                   - "True": { message: "Duplicate User detected.  Please use a different username." }
              validEmail:
                   - "True": { message: "Duplicate email detected. Please use a different email." }

fosuserbundle中内置了唯一的验证器,但我一直无法弄清楚如何使用它们.

There are built in unique validators in the fosuserbundle but I haven't been able to figure out how to use them.

推荐答案

我知道这是一个老问题,但是我只需要解决这个问题,所以我想我会分享我的解决方案.

I know that this is an old question but I've just had to work this out so I thought I would share my solution.

我不想使用任何捆绑软件来处理我的用户,因此这是我的手动方法:

I prefer not to use any bundles to handle my users so this is my manual approach:

<?php
namespace MyCorp\CorpBundle\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/** User - Represent a User object */
class User implements AdvancedUserInterface {

    /** @var integer $id */
    private $id;

    /** @var string $email */
    private $email;

    /** Constructor, Getters, Setters etc... */

    /** Set a list of rules to use when validating an instance of this Class 
        @param Symfony\Component\Validator\Mapping\ClassMetadata $metadata */
    public static function loadValidatorMetadata(ClassMetadata $metadata) {

        $metadata->addPropertyConstraint('email', new MaxLength(255));
        $metadata->addPropertyConstraint('email', new NotBlank());
        $metadata->addPropertyConstraint('email', new Email());

        $metadata->addConstraint(new UniqueEntity(array(
            "fields" => "email", 
            "message" => "This email address is already in use")
        ));

    }

}

如您所见,我在模型本身中定义了验证. Symfony将调用loadValidatorMetadata让您加载验证器.

As you can see I define my validation in the model itself. Symfony will call loadValidatorMetadata to let you load validators.

这篇关于Symfony 2独特的验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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