-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Validator Documentation for Collection constraint Required/Optional options #2432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
779f0e7
a7e150a
b659e24
e83d16f
3f38483
2938e6c
f826f60
2d4eb95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,75 @@ the above example, the ``allowMissingFields`` option was set to true, meaning | |
that if either of the ``personal_email`` or ``short_bio`` elements were missing | ||
from the ``$personalData`` property, no validation error would occur. | ||
|
||
.. versionadded:: 2.1 | ||
The ``Required`` and ``Optional`` constraint classes were added to give additional | ||
flexibility over the existing ``allowExtraFields`` and ``allowMissingFields`` options | ||
for fields within a Collection. | ||
|
||
Required and Optional Field Constraints | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the heading line should be as long as the heading title |
||
|
||
Constraints for fields within a collection can be wrapped in the ``Required`` or | ||
``Optional`` constraint to control whether they should always be applied (``Required``) | ||
or only applied when the field is present (``Optional``). | ||
|
||
For example, if you want to require that the ``personal_email`` field of the | ||
``profileData`` array is not blank and is a valid email but the ``alternate_email`` | ||
field is optional but must be a valid email if supplied, you can do the following: | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/Acme/BlogBundle/Entity/Author.php | ||
namespace Acme\BlogBundle\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
use Symfony\Component\Validator\Constraints\Collection\Optional; | ||
use Symfony\Component\Validator\Constraints\Collection\Required; | ||
|
||
class Author | ||
{ | ||
/** | ||
* @Assert\Collection( | ||
* fields={ | ||
* "personal_email" = @Required({@Assert\NotBlank, @Assert\Email}), | ||
* "alternate_email" = @Optional({@Assert\Email}), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to have the 2 |
||
* } | ||
* ) | ||
*/ | ||
protected $profileData = array( | ||
'personal_email' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. every array element should end with a comma. |
||
); | ||
} | ||
|
||
.. code-block:: php | ||
|
||
// src/Acme/BlogBundle/Entity/Author.php | ||
namespace Acme\BlogBundle\Entity; | ||
|
||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
use Symfony\Component\Validator\Constraints\Collection\Optional; | ||
use Symfony\Component\Validator\Constraints\Collection\Required; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as you alias some namespaces as Assert, I don't think you should alias these 2 apart. Just use:
|
||
|
||
class Author | ||
{ | ||
private $options = array(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be |
||
|
||
public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
{ | ||
$metadata->addPropertyConstraint('profileData', new Assert\Collection(array( | ||
'fields' => array( | ||
'personal_email' => new Required(array(new Assert\NotBlank(), new Assert\Email())), | ||
'alternate_email' => new Optional(array(new Assert\Email())), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line up the |
||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a missing comma here |
||
))); | ||
} | ||
} | ||
|
||
Even without ``allowMissingFields`` set to true, you can now omit the ``alternate_email`` property complete from the profileData array, since | ||
it is ``Optional``. However, if the the ``personal_email`` field does not exist in the array there will be a | ||
constraint violation that the field is missing, since it is ``Required``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. some line breaking missing. |
||
|
||
Options | ||
------- | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these versionadded blocks needs to be as short and descriptive as possible. I prefer to use
The Required and Optional constraints are new to Symfony 2.1
(to be consistent with other versionadded blocks)