@@ -18,8 +18,8 @@ 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 `` 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 ``.
23
23
24
24
.. configuration-block ::
25
25
@@ -32,6 +32,11 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
32
32
33
33
class Author
34
34
{
35
+ /**
36
+ * @Assert\Type("Ramsey\Uuid\UuidInterface")
37
+ */
38
+ protected $id;
39
+
35
40
/**
36
41
* @Assert\Type("string")
37
42
*/
@@ -51,6 +56,9 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
51
56
# config/validator/validation.yaml
52
57
App\Entity\Author :
53
58
properties :
59
+ id :
60
+ - Type : Ramsey\Uuid\UuidInterface
61
+
54
62
firstName :
55
63
- Type : string
56
64
@@ -68,6 +76,11 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
68
76
xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
69
77
70
78
<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 >
71
84
<property name =" firstName" >
72
85
<constraint name =" Type" >
73
86
<option name =" type" >string</option >
@@ -87,13 +100,16 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
87
100
// src/Entity/Author.php
88
101
namespace App\Entity;
89
102
103
+ use Ramsey\Uuid\UuidInterface;
90
104
use Symfony\Component\Validator\Constraints as Assert;
91
105
use Symfony\Component\Validator\Mapping\ClassMetadata;
92
106
93
107
class Author
94
108
{
95
109
public static function loadValidatorMetadata(ClassMetadata $metadata)
96
110
{
111
+ $metadata->addPropertyConstraint('id', new Assert\Type(UuidInterface::class));
112
+
97
113
$metadata->addPropertyConstraint('firstName', new Assert\Type('string'));
98
114
99
115
$metadata->addPropertyConstraint('age', new Assert\Type([
133
149
134
150
**type **: ``string `` [:ref: `default option <validation-default-option >`]
135
151
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):
138
154
139
155
* :phpfunction: `array <is_array> `
140
156
* :phpfunction: `bool <is_bool> `
0 commit comments