Skip to content
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

Improve Choice Validation Constraint Example #5233

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
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
16 changes: 9 additions & 7 deletions book/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ Some constraints, like :doc:`NotBlank </reference/constraints/NotBlank>`,
are simple whereas others, like the :doc:`Choice </reference/constraints/Choice>`
constraint, have several configuration options available. Suppose that the
``Author`` class has another property called ``gender`` that can be set to either
"male" or "female":
"male", "female" or "other":

.. configuration-block::

Expand All @@ -344,7 +344,7 @@ constraint, have several configuration options available. Suppose that the
{
/**
* @Assert\Choice(
* choices = { "male", "female" },
* choices = { "male", "female", "other" },
* message = "Choose a valid gender."
* )
*/
Expand All @@ -359,7 +359,7 @@ constraint, have several configuration options available. Suppose that the
AppBundle\Entity\Author:
properties:
gender:
- Choice: { choices: [male, female], message: Choose a valid gender. }
- Choice: { choices: [male, female, other], message: Choose a valid gender. }
# ...

.. code-block:: xml
Expand All @@ -376,6 +376,7 @@ constraint, have several configuration options available. Suppose that the
<option name="choices">
<value>male</value>
<value>female</value>
<value>other</value>
</option>
<option name="message">Choose a valid gender.</option>
</constraint>
Expand Down Expand Up @@ -404,7 +405,7 @@ constraint, have several configuration options available. Suppose that the
// ...

$metadata->addPropertyConstraint('gender', new Assert\Choice(array(
'choices' => array('male', 'female'),
'choices' => array('male', 'female', 'other'),
'message' => 'Choose a valid gender.',
)));
}
Expand All @@ -429,7 +430,7 @@ options can be specified in this way.
class Author
{
/**
* @Assert\Choice({"male", "female"})
* @Assert\Choice({"male", "female", "other"})
*/
protected $gender;

Expand All @@ -442,7 +443,7 @@ options can be specified in this way.
AppBundle\Entity\Author:
properties:
gender:
- Choice: [male, female]
- Choice: [male, female, other]
# ...

.. code-block:: xml
Expand All @@ -458,6 +459,7 @@ options can be specified in this way.
<constraint name="Choice">
<value>male</value>
<value>female</value>
<value>other</value>
</constraint>
</property>

Expand All @@ -483,7 +485,7 @@ options can be specified in this way.

$metadata->addPropertyConstraint(
'gender',
new Assert\Choice(array('male', 'female'))
new Assert\Choice(array('male', 'female', 'other'))
);
}
}
Expand Down