Esta restricción se utiliza para permitir la validación de objetos que se incrustan como propiedades en un objeto que se está validando. Esto te permite validar un objeto y todos los subobjetos asociados con él.
Aplica a | propiedad o método |
Opciones | |
Clase | Symfony\Component\Validator\Constraints\Type |
El siguiente ejemplo, crea dos clases Autor y Domicilio y ambas tienen restricciones en sus propiedades. Además, Autor almacena una instancia de Domicilio en la propiedad $address.
// src/Acme/HelloBundle/Entity/Address.php
namespace Amce\HelloBundle\Entity;
class Address
{
protected $street;
protected $zipCode;
}
// src/Acme/HelloBundle/Entity/Author.php
namespace Amce\HelloBundle\Entity;
class Author
{
protected $firstName;
protected $lastName;
protected $address;
}
# src/Acme/HelloBundle/Resources/config/validation.yml
Acme\HelloBundle\Entity\Address:
properties:
street:
- NotBlank: ~
zipCode:
- NotBlank: ~
- Length:
max: 5
Acme\HelloBundle\Entity\Author:
properties:
firstName:
- NotBlank: ~
- Length:
min: 4
lastName:
- NotBlank: ~
// src/Acme/HelloBundle/Entity/Address.php
namespace Acme\HelloBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Address
{
/**
* @Assert\NotBlank()
*/
protected $street;
/**
* @Assert\NotBlank
* @Assert\Length(max = "5")
*/
protected $zipCode;
}
// src/Acme/HelloBundle/Entity/Author.php
namespace Acme\HelloBundle\Entity;
class Autor
{
/**
* @Assert\NotBlank
* @Assert\Length(min = "4")
*/
protected $firstName;
/**
* @Assert\NotBlank
*/
protected $lastName;
protected $address;
}
<!-- src/Acme/HelloBundle/Resources/config/validation.xml -->
<class name="Acme\HelloBundle\Entity\Address">
<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="Acme\HelloBundle\Entity\Author">
<property name="firstName">
<constraint name="NotBlank" />
<constraint name="Length">
<option name="min">4</option>
</constraint>
</property>
<property name="lastName">
<constraint name="NotBlank" />
</property>
</class>
// src/Acme/HelloBundle/Entity/Address.php
namespace Acme\HelloBundle\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;
class Address
{
protected $street;
protected $zipCode;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('street', new Assert\NotBlank());
$metadata->addPropertyConstraint('zipCode', new Assert\NotBlank());
$metadata->addPropertyConstraint(
'zipCode',
new Assert\Length(array("max" => 5)));
}
}
// src/Acme/HelloBundle/Entity/Author.php
namespace Acme\HelloBundle\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;
class Autor
{
protected $firstName;
protected $lastName;
protected $address;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('firstName', new Assert\NotBlank());
$metadata->addPropertyConstraint('firstName', new Assert\Length(array("min" => 4)));
$metadata->addPropertyConstraint('lastName', new Assert\NotBlank());
}
}
Con esta asignación puedes validar satisfactoriamente a un autor con una dirección no válida. Para evitarlo, agrega la restricción Valid a la propiedad $address.
# src/Acme/HelloBundle/Resources/config/validation.yml
Acme\HelloBundle\Author:
properties:
address:
- Valid: ~
// src/Acme/HelloBundle/Entity/Author.php
namespace Acme\HelloBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Autor
{
/**
* @Assert\Valid
*/
protected $address;
}
<!-- src/Acme/HelloBundle/Resources/config/validation.xml -->
<class name="Acme\HelloBundle\Entity\Author">
<property name="address">
<constraint name="Valid" />
</property>
</class>
// src/Acme/HelloBundle/Entity/Author.php
namespace Acme\HelloBundle\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;
class Autor
{
protected $address;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('address', new Assert\Valid());
}
}
Si ahora validas a un autor con una dirección no válida, puedes ver que la validación de los campos Address falla:
Acme\HelloBundle\Author.address.zipCode:
Este valor es demasiado largo. Debe tener 5 caracteres o menos