@@ -18,8 +18,8 @@ Validator :class:`Symfony\\Component\\Validator\\Constraints\\TypeValidator`
1818Basic Usage
1919-----------
2020
21- This will check if ``firstName `` is of type `` string `` and that `` age `` is an
22- ``integer ``.
21+ This will check that ``id `` is an instance of `` Ramsey\Uuid\UuidInterface ``,
22+ ``firstName `` is of type `` string `` and `` age `` is an `` integer ``.
2323
2424.. configuration-block ::
2525
@@ -32,6 +32,11 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
3232
3333 class Author
3434 {
35+ /**
36+ * @Assert\Type("Ramsey\Uuid\UuidInterface")
37+ */
38+ protected $id;
39+
3540 /**
3641 * @Assert\Type("string")
3742 */
@@ -51,6 +56,9 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
5156 # config/validator/validation.yaml
5257 App\Entity\Author :
5358 properties :
59+ id :
60+ - Type : Ramsey\Uuid\UuidInterface
61+
5462 firstName :
5563 - Type : string
5664
@@ -68,6 +76,11 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
6876 xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
6977
7078 <class name =" App\Entity\Author" >
79+ <property name =" id" >
80+ <constraint name =" Type" >
81+ <option name =" type" >Ramsey\Uuid\UuidInterface</option >
82+ </constraint >
83+ </property >
7184 <property name =" firstName" >
7285 <constraint name =" Type" >
7386 <option name =" type" >string</option >
@@ -87,13 +100,16 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
87100 // src/Entity/Author.php
88101 namespace App\Entity;
89102
103+ use Ramsey\Uuid\UuidInterface;
90104 use Symfony\Component\Validator\Constraints as Assert;
91105 use Symfony\Component\Validator\Mapping\ClassMetadata;
92106
93107 class Author
94108 {
95109 public static function loadValidatorMetadata(ClassMetadata $metadata)
96110 {
111+ $metadata->addPropertyConstraint('id', new Assert\Type(UuidInterface::class));
112+
97113 $metadata->addPropertyConstraint('firstName', new Assert\Type('string'));
98114
99115 $metadata->addPropertyConstraint('age', new Assert\Type([
133149
134150**type **: ``string `` [:ref: `default option <validation-default-option >`]
135151
136- This required option is the fully qualified class name or one of the PHP
137- datatypes as determined by PHP's ``is_() `` functions.
152+ This required option is either the FQCN ( fully qualified class name) of some PHP
153+ class/interface or a valid PHP datatype (checked by PHP's ``is_() `` functions):
138154
139155* :phpfunction: `array <is_array> `
140156* :phpfunction: `bool <is_bool> `
0 commit comments