@@ -79,7 +79,7 @@ of a class ``Author`` to have at least 3 characters.
79
79
</property >
80
80
</class >
81
81
82
- .. code-block :: php
82
+ .. code-block :: php-annotations
83
83
84
84
// Application/HelloBundle/Author.php
85
85
class Author
@@ -94,7 +94,28 @@ of a class ``Author`` to have at least 3 characters.
94
94
* @validation:NotBlank()
95
95
* @validation:MinLength(3)
96
96
*/
97
+ private $lastName;
98
+ }
99
+
100
+ .. code-block :: php
101
+
102
+ // Application/HelloBundle/Author.php
103
+ use Symfony\Components\Validator\Constraints\NotBlank;
104
+ use Symfony\Components\Validator\Constraints\MinLength;
105
+
106
+ class Author
107
+ {
97
108
private $firstName;
109
+
110
+ private $lastName;
111
+
112
+ public static function loadMetadata(ClassMetadata $metadata)
113
+ {
114
+ $metadata->addPropertyConstraint('firstName', new NotBlank());
115
+ $metadata->addPropertyConstraint('firstName', new MinLength(3));
116
+ $metadata->addPropertyConstraint('lastName', new NotBlank());
117
+ $metadata->addPropertyConstraint('lastName', new MinLength(3));
118
+ }
98
119
}
99
120
100
121
Getters
@@ -133,7 +154,7 @@ generated token is correct:
133
154
</getter >
134
155
</class >
135
156
136
- .. code-block :: php
157
+ .. code-block :: php-annotations
137
158
138
159
// Application/HelloBundle/Author.php
139
160
class Author
@@ -147,6 +168,27 @@ generated token is correct:
147
168
}
148
169
}
149
170
171
+ .. code-block :: php
172
+
173
+ // Application/HelloBundle/Author.php
174
+ use Symfony\Components\Validator\Constraints\AssertTrue;
175
+
176
+ class Author
177
+ {
178
+
179
+ public static function loadMetadata(ClassMetadata $metadata)
180
+ {
181
+ $metadata->addGetterConstraint('tokenValid', new AssertTrue(array(
182
+ 'message' => 'The token is invalid',
183
+ )));
184
+ }
185
+
186
+ public function isTokenValid()
187
+ {
188
+ // return true or false
189
+ }
190
+ }
191
+
150
192
.. note ::
151
193
152
194
The keen-eyed among you will have noticed that the prefix of the getter
@@ -161,7 +203,9 @@ You can create a custom constraint by extending the base constraint class,
161
203
:class: `Symfony\\ Component\\ Validator\\ Constraint `. Options for your
162
204
constraint are represented by public properties on the constraint class. For
163
205
example, the ``Url `` constraint includes ``message `` and ``protocols ``
164
- properties::
206
+ properties:
207
+
208
+ .. code-block :: php
165
209
166
210
namespace Symfony\Component\Validator\Constraints;
167
211
@@ -174,7 +218,9 @@ properties::
174
218
As you can see, a constraint class is fairly minimal. The actual validation is
175
219
performed by a another "constraint validator" class. Which constraint
176
220
validator is specified by the constraint's ``validatedBy() `` method, which
177
- includes some simple default logic::
221
+ includes some simple default logic:
222
+
223
+ .. code-block :: php
178
224
179
225
// in the base Symfony\Component\Validator\Constraint class
180
226
public function validatedBy()
0 commit comments