-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
[Reference][Constraints] document the validation payload option #4724
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ Validation | |
:maxdepth: 2 | ||
|
||
custom_constraint | ||
severity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
.. index:: | ||
single: Validation; Error Levels | ||
single: Validation; Payload | ||
|
||
How to Handle Different Error Levels | ||
==================================== | ||
|
||
Sometimes, you may want to display constraint validation error messages differently | ||
based on some rules. For example, you have a registration form for new users | ||
where they enter some personal information and choose their authentication | ||
credentials. They would have to choose a username and a secure password, | ||
but providing bank account information would be optional. Nonetheless, you | ||
want to make sure that these optional data, if entered, are still valid, | ||
but display them differently. | ||
|
||
The process to achieve this behavior consists of two steps: | ||
|
||
#. Apply different error levels to the validation constraints; | ||
#. Customize your error messages depending on the configured error level. | ||
|
||
1. Assigning the Error Level | ||
---------------------------- | ||
|
||
.. versionadded:: 2.6 | ||
The ``payload`` option was introduced in Symfony 2.6. | ||
|
||
Use the ``payload`` option to configure the error level for each constraint: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/AppBundle/Entity/User.php | ||
namespace AppBundle\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class User | ||
{ | ||
/** | ||
* @Assert\NotBlank(payload = {severity = "error"}) | ||
*/ | ||
protected $username; | ||
|
||
/** | ||
* @Assert\NotBlank(payload = {severity = "error"}) | ||
*/ | ||
protected $password; | ||
|
||
/** | ||
* @Assert\Iban(payload = {severity = "warning"}) | ||
*/ | ||
protected $bankAccountNumber; | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# src/AppBundle/Resources/config/validation.yml | ||
AppBundle\Entity\User: | ||
properties: | ||
username: | ||
- NotBlank: | ||
payload: | ||
severity: error | ||
password: | ||
- NotBlank: | ||
payload: | ||
severity: error | ||
bankAccountNumber: | ||
- Iban: | ||
payload: | ||
severity: warning | ||
|
||
.. code-block:: xml | ||
|
||
<!-- src/AppBundle/Resources/config/validation.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> | ||
|
||
<class name="AppBundle\Entity\User"> | ||
<property name="username"> | ||
<constraint name="NotBlank"> | ||
<option name="payload"> | ||
<value key="severity">error</value> | ||
</option> | ||
</constraint> | ||
</property> | ||
<property name="password"> | ||
<constraint name="NotBlank"> | ||
<option name="payload"> | ||
<value key="severity">error</value> | ||
</option> | ||
</constraint> | ||
</property> | ||
<property name="bankAccountNumber"> | ||
<constraint name="Iban"> | ||
<option name="payload"> | ||
<value key="severity">warning</value> | ||
</option> | ||
</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/AppBundle/Entity/User.php | ||
namespace AppBundle\Entity; | ||
|
||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class User | ||
{ | ||
public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
{ | ||
$metadata->addPropertyConstraint('username', new Assert\NotBlank(array( | ||
'payload' => array('severity' => 'error'), | ||
))); | ||
$metadata->addPropertyConstraint('password', new Assert\NotBlank(array( | ||
'payload' => array('severity' => 'error'), | ||
))); | ||
$metadata->addPropertyConstraint('bankAccountNumber', new Assert\Iban(array( | ||
'payload' => array('severity' => 'warning'), | ||
))); | ||
} | ||
} | ||
|
||
2. Customize the Error Message Template | ||
--------------------------------------- | ||
|
||
.. versionadded:: 2.6 | ||
The ``getConstraint()`` method in the ``ConstraintViolation`` class was | ||
introduced in Symfony 2.6. | ||
|
||
When validating the ``User`` object failed, you can retrieve the constraint | ||
that caused a particular failure using the | ||
:method:`Symfony\\Component\\Validator\\ConstraintViolation::getConstraint` | ||
method. Each constraint exposes the attached payload as a public property:: | ||
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. one 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 double colon is a shortcut for the default language so that you don't need to add a |
||
|
||
// a constraint validation failure, instance of | ||
// Symfony\Component\Validator\ConstraintViolation | ||
$constraintViolation = ...; | ||
$constraint = $constraintViolation->getConstraint(); | ||
$severity = isset($constraint->payload['severity']) ? $constraint->payload['severity'] : null; | ||
|
||
For example, you can leverage this to customize the ``form_errors`` block | ||
such that the severity is added as an additional HTML class: | ||
|
||
.. code-block:: html+jinja | ||
|
||
{%- block form_errors -%} | ||
{%- if errors|length > 0 -%} | ||
<ul> | ||
{%- for error in errors -%} | ||
{% if error.cause.constraint.payload.severity is defined %} | ||
{% set severity = error.cause.constraint.payload.severity %} | ||
{% endif %} | ||
<li{% if severity is defined %} class="{{ severity }}"{% endif %}>{{ error.message }}</li> | ||
{%- endfor -%} | ||
</ul> | ||
{%- endif -%} | ||
{%- endblock form_errors -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
annotations should now be the first
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.
done