发布于 2015-08-27 16:41:50 | 157 次阅读 | 评论: 0 | 来源: 网络整理
Validates that a particular field (or fields) in a Doctrine entity is (are) unique. This is commonly used, for example, to prevent a new user to register using an email address that already exists in the system.
Applies to | class |
Options | |
Class | UniqueEntity |
Validator | UniqueEntityValidator |
Suppose you have an AcmeUserBundle bundle with a User
entity that has an
email
field. You can use the UniqueEntity
constraint to guarantee that
the email
field remains unique between all of the constraints in your user
table:
# src/Acme/UserBundle/Resources/config/validation.yml
AcmeUserBundleEntityAuthor:
constraints:
- SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity: email
properties:
email:
- Email: ~
// Acme/UserBundle/Entity/Author.php
namespace AcmeUserBundleEntity;
use SymfonyComponentValidatorConstraints as Assert;
use DoctrineORMMapping as ORM;
// DON'T forget this use statement!!!
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
/**
* @ORMEntity
* @UniqueEntity("email")
*/
class Author
{
/**
* @var string $email
*
* @ORMColumn(name="email", type="string", length=255, unique=true)
* @AssertEmail()
*/
protected $email;
// ...
}
<!-- src/Acme/AdministrationBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="AcmeUserBundleEntityAuthor">
<constraint name="SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity">
<option name="fields">email</option>
</constraint>
<property name="email">
<constraint name="Email" />
</property>
</class>
</constraint-mapping>
// Acme/UserBundle/Entity/User.php
namespace AcmeUserBundleEntity;
use SymfonyComponentValidatorConstraints as Assert;
// DON'T forget this use statement!!!
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new UniqueEntity(array(
'fields' => 'email',
)));
$metadata->addPropertyConstraint('email', new AssertEmail());
}
}
type: array
| string
[default option]
This required option is the field (or list of fields) on which this entity
should be unique. For example, if you specified both the email
and name
field in a single UniqueEntity
constraint, then it would enforce that
the combination value where unique (e.g. two users could have the same email,
as long as they don’t have the same name also).
If you need to require two fields to be individually unique (e.g. a unique
email
and a unique username
), you use two UniqueEntity
entries,
each with a single field.
type: string
default: This value is already used.
The message that’s displayed when this constraint fails.
type: string
The name of the entity manager to use for making the query to determine the uniqueness. If it’s left blank, the correct entity manager will be determined for this class. For that reason, this option should probably not need to be used.
type: string
default: findBy
The name of the repository method to use for making the query to determine the
uniqueness. If it’s left blank, the findBy
method will be used. This
method should return a countable result.
type: string
default: The name of the first field in fields
If the entity violates the constraint the error message is bound to the first field in fields. If there is more than one field, you may want to map the error message to another field.
Consider this example:
# src/Acme/AdministrationBundle/Resources/config/validation.yml
AcmeAdministrationBundleEntityService:
constraints:
- SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity:
fields: [host, port]
errorPath: port
message: 'This port is already in use on that host.'
// src/Acme/AdministrationBundle/Entity/Service.php
namespace AcmeAdministrationBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
/**
* @ORMEntity
* @UniqueEntity(
* fields={"host", "port"},
* errorPath="port",
* message="This port is already in use on that host."
* )
*/
class Service
{
/**
* @ORMManyToOne(targetEntity="Host")
*/
public $host;
/**
* @ORMColumn(type="integer")
*/
public $port;
}
<!-- src/Acme/AdministrationBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="AcmeAdministrationBundleEntityService">
<constraint name="SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity">
<option name="fields">
<value>host</value>
<value>port</value>
</option>
<option name="errorPath">port</option>
<option name="message">This port is already in use on that host.</option>
</constraint>
</class>
</constraint-mapping>
// src/Acme/AdministrationBundle/Entity/Service.php
namespace AcmeAdministrationBundleEntity;
use SymfonyComponentValidatorMappingClassMetadata;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
class Service
{
public $host;
public $port;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new UniqueEntity(array(
'fields' => array('host', 'port'),
'errorPath' => 'port',
'message' => 'This port is already in use on that host.',
)));
}
}
Now, the message would be bound to the port
field with this configuration.
type: Boolean
default: true
If this option is set to true
, then the constraint will allow multiple
entities to have a null
value for a field without failing validation.
If set to false
, only one null
value is allowed - if a second entity
also has a null
value, validation would fail.
type: mixed
default: null
2.6 新版功能: The payload
option was introduced in Symfony 2.6.
This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to.
For example, you may want to used several error levels to present failed constraint differently in the front-end depending on the severity of the error.