@@ -18,9 +18,10 @@ Validator :class:`Symfony\\Component\\Validator\\Constraints\\TypeValidator`
18
18
Basic Usage
19
19
-----------
20
20
21
- This will check if ``firstName `` is of type ``string `` (using :phpfunction: `is_string `
22
- PHP function), ``age `` is an ``integer `` (using :phpfunction: `is_int ` PHP
23
- function) and ``accessCode `` contains either only letters or only digits (using
21
+ This will check if ``id `` is an instance of ``Ramsey\Uuid\UuidInterface ``,
22
+ ``firstName `` is of type ``string `` (using :phpfunction: `is_string ` PHP function),
23
+ ``age `` is an ``integer `` (using :phpfunction: `is_int ` PHP function) and
24
+ ``accessCode `` contains either only letters or only digits (using
24
25
:phpfunction: `ctype_alpha ` and :phpfunction: `ctype_digit ` PHP functions).
25
26
26
27
.. configuration-block ::
@@ -34,6 +35,11 @@ function) and ``accessCode`` contains either only letters or only digits (using
34
35
35
36
class Author
36
37
{
38
+ /**
39
+ * @Assert\Type("Ramsey\Uuid\UuidInterface")
40
+ */
41
+ protected $id;
42
+
37
43
/**
38
44
* @Assert\Type("string")
39
45
*/
@@ -58,6 +64,9 @@ function) and ``accessCode`` contains either only letters or only digits (using
58
64
# config/validator/validation.yaml
59
65
App\Entity\Author :
60
66
properties :
67
+ id :
68
+ - Type : Ramsey\Uuid\UuidInterface
69
+
61
70
firstName :
62
71
- Type : string
63
72
@@ -79,6 +88,11 @@ function) and ``accessCode`` contains either only letters or only digits (using
79
88
xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
80
89
81
90
<class name =" App\Entity\Author" >
91
+ <property name =" id" >
92
+ <constraint name =" Type" >
93
+ <option name =" type" >Ramsey\Uuid\UuidInterface</option >
94
+ </constraint >
95
+ </property >
82
96
<property name =" firstName" >
83
97
<constraint name =" Type" >
84
98
<option name =" type" >string</option >
@@ -106,13 +120,16 @@ function) and ``accessCode`` contains either only letters or only digits (using
106
120
// src/Entity/Author.php
107
121
namespace App\Entity;
108
122
123
+ use Ramsey\Uuid\UuidInterface;
109
124
use Symfony\Component\Validator\Constraints as Assert;
110
125
use Symfony\Component\Validator\Mapping\ClassMetadata;
111
126
112
127
class Author
113
128
{
114
129
public static function loadValidatorMetadata(ClassMetadata $metadata)
115
130
{
131
+ $metadata->addPropertyConstraint('id', new Assert\Type(UuidInterface::class));
132
+
116
133
$metadata->addPropertyConstraint('firstName', new Assert\Type('string'));
117
134
118
135
$metadata->addPropertyConstraint('age', new Assert\Type([
167
184
in Symfony 4.4.
168
185
169
186
This required option defines the type or collection of types allowed for the
170
- given value. Each type is defined as the fully qualified class name or one of
171
- the PHP datatypes as determined by PHP's ``is_* () `` functions.
187
+ given value. Each type is is either the FQCN ( fully qualified class name) of some
188
+ PHP class/interface or a valid PHP datatype (checked by PHP's ``is_() `` functions):
172
189
173
190
* :phpfunction: `array <is_array> `
174
191
* :phpfunction: `bool <is_bool> `
0 commit comments