@@ -21,8 +21,8 @@ option to validate this.
21
21
Basic Usage
22
22
-----------
23
23
24
- This will check if ``firstName `` is of type `` string `` and that `` age `` is an
25
- ``integer ``.
24
+ This will check that ``id `` is an instance of `` Ramsey\Uuid\UuidInterface ``,
25
+ ``firstName `` is of type `` string `` and `` age `` is an `` integer ``.
26
26
27
27
.. configuration-block ::
28
28
@@ -35,6 +35,11 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
35
35
36
36
class Author
37
37
{
38
+ /**
39
+ * @Assert\Type("Ramsey\Uuid\UuidInterface")
40
+ */
41
+ protected $id;
42
+
38
43
/**
39
44
* @Assert\Type("string")
40
45
*/
@@ -54,6 +59,9 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
54
59
# src/AppBundle/Resources/config/validation.yml
55
60
AppBundle\Entity\Author :
56
61
properties :
62
+ id :
63
+ - Type : Ramsey\Uuid\UuidInterface
64
+
57
65
firstName :
58
66
- Type : string
59
67
@@ -71,6 +79,11 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
71
79
xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
72
80
73
81
<class name =" AppBundle\Entity\Author" >
82
+ <property name =" id" >
83
+ <constraint name =" Type" >
84
+ <option name =" type" >Ramsey\Uuid\UuidInterface</option >
85
+ </constraint >
86
+ </property >
74
87
<property name =" firstName" >
75
88
<constraint name =" Type" >
76
89
<option name =" type" >string</option >
@@ -90,13 +103,16 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
90
103
// src/AppBundle/Entity/Author.php
91
104
namespace AppBundle\Entity;
92
105
106
+ use Ramsey\Uuid\UuidInterface;
93
107
use Symfony\Component\Validator\Constraints as Assert;
94
108
use Symfony\Component\Validator\Mapping\ClassMetadata;
95
109
96
110
class Author
97
111
{
98
112
public static function loadValidatorMetadata(ClassMetadata $metadata)
99
113
{
114
+ $metadata->addPropertyConstraint('id', new Assert\Type(UuidInterface::class));
115
+
100
116
$metadata->addPropertyConstraint('firstName', new Assert\Type('string'));
101
117
102
118
$metadata->addPropertyConstraint('age', new Assert\Type([
@@ -106,10 +122,6 @@ This will check if ``firstName`` is of type ``string`` and that ``age`` is an
106
122
}
107
123
}
108
124
109
- .. tip ::
110
-
111
- Type value can be class's or interface's fully qualified class name
112
-
113
125
Options
114
126
-------
115
127
120
132
121
133
**type **: ``string `` [:ref: `default option <validation-default-option >`]
122
134
123
- This required option is the fully qualified class name or one of the PHP
124
- datatypes as determined by PHP's ``is_() `` functions.
135
+ This required option is either the FQCN ( fully qualified class name) of some PHP
136
+ class/interface or a valid PHP datatype (checked by PHP's ``is_() `` functions):
125
137
126
138
* :phpfunction: `array <is_array> `
127
139
* :phpfunction: `bool <is_bool> `
0 commit comments