Valida que un valor es de un tipo de dato específico. Por ejemplo, si una variable debe ser un array, puedes utilizar esta restricción con la opción type para validarla.
| Aplica a | propiedad o método |
| Opciones | |
| Clase | Symfony\Component\Validator\Constraints\Type |
| Validador | Symfony\Component\Validator\Constraints\TypeValidator |
# src/BlogBundle/Resources/config/validation.yml
Acme\BlogBundle\Entity\Author:
properties:
edad:
- Type:
type: integer
message: The value {{ value }} is not a valid {{ type }}.
// src/Acme/BlogBundle/Entity/Author.php
namespace Acme\BlogBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Autor
{
/**
* @Assert\Type(type="integer", message="The value {{ value }} is not a valid {{ type }}.")
*/
protected $age;
}
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\Author">
<property name="age">
<constraint name="Type">
<option name="type">integer</option>
<option name="message">The value {{ value }} is not a valid {{ type }}.</option>
</constraint>
</property>
</class>
// src/Acme/BlogBundle/Entity/Author.php
namespace Acme\BlogBundle\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;
class Autor
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('age', new Assert\Type(array(
'type' => 'integer',
'message' => 'The value {{ value }} is not a valid {{ type }}.',
)));
}
}
tipo: string [default option]
Esta opción requerida es el nombre de clase completo o uno de los tipos de datos PHP según lo determinado por las funciones is_ de PHP.
tipo: string predefinido: This value should be of type {{ type }} (Este valor debe ser de tipo {{ type }})
El mensaje si el dato subyacente no es del tipo dado.