Skip to content

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

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions reference/constraints/Collection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

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)


Required and Optional Field Constraints
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy link
Member

Choose a reason for hiding this comment

The 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}),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to have the 2 = lined up with eachother.

* }
* )
*/
protected $profileData = array(
'personal_email'
Copy link
Member

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The 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:

Assert\Collection\Optional


class Author
{
private $options = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be protected $profileData = array('personal_email');.


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())),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line up the => aswell.

)
Copy link
Member

Choose a reason for hiding this comment

The 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``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some line breaking missing.


Options
-------

Expand Down