发布于 2015-08-27 16:40:19 | 114 次阅读 | 评论: 0 | 来源: 网络整理
This constraint is used to enable validation on objects that are embedded as properties on an object being validated. This allows you to validate an object and all sub-objects associated with it.
Applies to | property or method |
Options | |
Class | Valid |
小技巧
By default the error_bubbling
option is enabled for the
collection Field Type,
which passes the errors to the parent form. If you want to attach
the errors to the locations where they actually occur you have to
set error_bubbling
to false
.
In the following example, create two classes Author
and Address
that both have constraints on their properties. Furthermore, Author
stores
an Address
instance in the $address
property.
// src/Acme/HelloBundle/Entity/Address.php
namespace AcmeHelloBundleEntity;
class Address
{
protected $street;
protected $zipCode;
}
// src/Acme/HelloBundle/Entity/Author.php
namespace AcmeHelloBundleEntity;
class Author
{
protected $firstName;
protected $lastName;
protected $address;
}
# src/Acme/HelloBundle/Resources/config/validation.yml
AcmeHelloBundleEntityAddress:
properties:
street:
- NotBlank: ~
zipCode:
- NotBlank: ~
- Length:
max: 5
AcmeHelloBundleEntityAuthor:
properties:
firstName:
- NotBlank: ~
- Length:
min: 4
lastName:
- NotBlank: ~
// src/Acme/HelloBundle/Entity/Address.php
namespace AcmeHelloBundleEntity;
use SymfonyComponentValidatorConstraints as Assert;
class Address
{
/**
* @AssertNotBlank()
*/
protected $street;
/**
* @AssertNotBlank
* @AssertLength(max = 5)
*/
protected $zipCode;
}
// src/Acme/HelloBundle/Entity/Author.php
namespace AcmeHelloBundleEntity;
use SymfonyComponentValidatorConstraints as Assert;
class Author
{
/**
* @AssertNotBlank
* @AssertLength(min = 4)
*/
protected $firstName;
/**
* @AssertNotBlank
*/
protected $lastName;
protected $address;
}
<!-- src/Acme/HelloBundle/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="AcmeHelloBundleEntityAddress">
<property name="street">
<constraint name="NotBlank" />
</property>
<property name="zipCode">
<constraint name="NotBlank" />
<constraint name="Length">
<option name="max">5</option>
</constraint>
</property>
</class>
<class name="AcmeHelloBundleEntityAuthor">
<property name="firstName">
<constraint name="NotBlank" />
<constraint name="Length">
<option name="min">4</option>
</constraint>
</property>
<property name="lastName">
<constraint name="NotBlank" />
</property>
</class>
</constraint-mapping>
// src/Acme/HelloBundle/Entity/Address.php
namespace AcmeHelloBundleEntity;
use SymfonyComponentValidatorMappingClassMetadata;
use SymfonyComponentValidatorConstraints as Assert;
class Address
{
protected $street;
protected $zipCode;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('street', new AssertNotBlank());
$metadata->addPropertyConstraint('zipCode', new AssertNotBlank());
$metadata->addPropertyConstraint('zipCode', new AssertLength(array("max" => 5)));
}
}
// src/Acme/HelloBundle/Entity/Author.php
namespace AcmeHelloBundleEntity;
use SymfonyComponentValidatorMappingClassMetadata;
use SymfonyComponentValidatorConstraints as Assert;
class Author
{
protected $firstName;
protected $lastName;
protected $address;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('firstName', new AssertNotBlank());
$metadata->addPropertyConstraint('firstName', new AssertLength(array("min" => 4)));
$metadata->addPropertyConstraint('lastName', new AssertNotBlank());
}
}
With this mapping, it is possible to successfully validate an author with an
invalid address. To prevent that, add the Valid
constraint to the $address
property.
# src/Acme/HelloBundle/Resources/config/validation.yml
AcmeHelloBundleEntityAuthor:
properties:
address:
- Valid: ~
// src/Acme/HelloBundle/Entity/Author.php
namespace AcmeHelloBundleEntity;
use SymfonyComponentValidatorConstraints as Assert;
class Author
{
/**
* @AssertValid
*/
protected $address;
}
<!-- src/Acme/HelloBundle/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="AcmeHelloBundleEntityAuthor">
<property name="address">
<constraint name="Valid" />
</property>
</class>
</constraint-mapping>
// src/Acme/HelloBundle/Entity/Author.php
namespace AcmeHelloBundleEntity;
use SymfonyComponentValidatorMappingClassMetadata;
use SymfonyComponentValidatorConstraints as Assert;
class Author
{
protected $address;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('address', new AssertValid());
}
}
If you validate an author with an invalid address now, you can see that the
validation of the Address
fields failed.
AcmeHelloBundleAuthor.address.zipCode:
This value is too long. It should have 5 characters or less.
type: boolean
default: true
If this constraint is applied to a property that holds an array of objects,
then each object in that array will be validated only if this option is set
to true
.
type: boolean
default: false
If this constraint is applied to a property that holds an array of objects,
then each object in that array will be validated recursively if this option is set
to true
.
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.