@@ -163,6 +163,75 @@ the above example, the ``allowMissingFields`` option was set to true, meaning
163
163
that if either of the ``personal_email `` or ``short_bio `` elements were missing
164
164
from the ``$personalData `` property, no validation error would occur.
165
165
166
+ .. versionadded :: 2.1
167
+ The ``Required `` and ``Optional `` constraint classes were added to give additional
168
+ flexibility over the existing ``allowExtraFields `` and ``allowMissingFields `` options
169
+ for fields within a Collection.
170
+
171
+ Required and Optional Field Constraints
172
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
173
+
174
+ Constraints for fields within a collection can be wrapped in the ``Required `` or
175
+ ``Optional `` constraint to control whether they should always be applied (``Required ``)
176
+ or only applied when the field is present (``Optional ``).
177
+
178
+ For example, if you want to require that the ``personal_email `` field of the
179
+ ``profileData `` array is not blank and is a valid email but the ``alternate_email ``
180
+ field is optional but must be a valid email if supplied, you can do the following:
181
+
182
+ .. code-block :: php-annotations
183
+
184
+ // src/Acme/BlogBundle/Entity/Author.php
185
+ namespace Acme\BlogBundle\Entity;
186
+
187
+ use Symfony\Component\Validator\Constraints as Assert;
188
+ use Symfony\Component\Validator\Constraints\Collection\Optional;
189
+ use Symfony\Component\Validator\Constraints\Collection\Required;
190
+
191
+ class Author
192
+ {
193
+ /**
194
+ * @Assert\Collection(
195
+ * fields={
196
+ * "personal_email" = @Required({@Assert\NotBlank, @Assert\Email}),
197
+ * "alternate_email" = @Optional({@Assert\Email}),
198
+ * }
199
+ * )
200
+ */
201
+ protected $profileData = array(
202
+ 'personal_email'
203
+ );
204
+ }
205
+
206
+ .. code-block :: php
207
+
208
+ // src/Acme/BlogBundle/Entity/Author.php
209
+ namespace Acme\BlogBundle\Entity;
210
+
211
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
212
+ use Symfony\Component\Validator\Constraints as Assert;
213
+ use Symfony\Component\Validator\Constraints\Collection\Optional;
214
+ use Symfony\Component\Validator\Constraints\Collection\Required;
215
+
216
+ class Author
217
+ {
218
+ private $options = array();
219
+
220
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
221
+ {
222
+ $metadata->addPropertyConstraint('profileData', new Assert\Collection(array(
223
+ 'fields' => array(
224
+ 'personal_email' => new Required(array(new Assert\NotBlank(), new Assert\Email())),
225
+ 'alternate_email' => new Optional(array(new Assert\Email())),
226
+ )
227
+ )));
228
+ }
229
+ }
230
+
231
+ Even without ``allowMissingFields `` set to true, you can now omit the ``alternate_email `` property complete from the profileData array, since
232
+ it is ``Optional ``. However, if the the ``personal_email `` field does not exist in the array there will be a
233
+ constraint violation that the field is missing, since it is ``Required ``.
234
+
166
235
Options
167
236
-------
168
237
0 commit comments