-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Validator] Add a constraint to sequentially validate a set of constraints #13206
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
OskarStark
merged 1 commit into
symfony:master
from
ogizanagi:validator-sequentially-constraint
Feb 24, 2020
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 hidden or 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 hidden or 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,142 @@ | ||
Sequentially | ||
============ | ||
|
||
This constraint allows you to apply a set of rules that should be validated | ||
step-by-step, allowing to interrupt the validation once the first violation is raised. | ||
|
||
As an alternative in situations ``Sequentially`` cannot solve, you may consider | ||
using :doc:`GroupSequence</validation/sequence_provider>` which allows more control. | ||
|
||
.. versionadded:: 5.1 | ||
|
||
The ``Sequentially`` constraint was introduced in Symfony 5.1. | ||
|
||
========== =================================================================== | ||
Applies to :ref:`property or method <validation-property-target>` | ||
Options - `constraints`_ | ||
- `groups`_ | ||
- `payload`_ | ||
Class :class:`Symfony\\Component\\Validator\\Constraints\\Sequentially` | ||
Validator :class:`Symfony\\Component\\Validator\\Constraints\\SequentiallyValidator` | ||
========== =================================================================== | ||
|
||
Basic Usage | ||
----------- | ||
|
||
Suppose that you have a ``Place`` object with an ``$address`` property which | ||
must match the following requirements: | ||
- it's a non-blank string | ||
- of at least 10 chars long | ||
- with a specific format | ||
- and geolocalizable using an external service | ||
|
||
In such situations, you may encounter three issues: | ||
- the ``Length`` or ``Regex`` constraints may fail hard with a :class:`Symfony\\Component\\Validator\\Exception\\UnexpectedValueException` | ||
exception if the actual value is not a string, as enforced by ``Type``. | ||
- you may end with multiple error messages for the same property | ||
- you may perform a useless and heavy external call to geolocalize the address, | ||
while the format isn't valid. | ||
|
||
You can validate each of these constraints sequentially to solve these issues: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/Localization/Place.php | ||
namespace App\Localization; | ||
|
||
use App\Validator\Constraints as AcmeAssert; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Place | ||
{ | ||
/** | ||
* @var string | ||
* | ||
* @Assert\Sequentially({ | ||
* @Assert\NotBlank(), | ||
* @Assert\Type("string"), | ||
* @Assert\Length(min=10), | ||
* @Assert\Regex(Place::ADDRESS_REGEX), | ||
* @AcmeAssert\Geolocalizable(), | ||
* }) | ||
*/ | ||
public $address; | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# config/validator/validation.yaml | ||
App\Localization\Place: | ||
properties: | ||
address: | ||
- Sequentially: | ||
- NotBlank: ~ | ||
- Type: string | ||
- Length: { min: 10 } | ||
- Regex: !php/const App\Localization\Place::ADDRESS_REGEX | ||
- App\Validator\Constraints\Geolocalizable: ~ | ||
|
||
.. code-block:: xml | ||
|
||
<!-- config/validator/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 https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> | ||
|
||
<class name="App\Localization\Place"> | ||
<property name="address"> | ||
<constraint name="Sequentially"> | ||
<constraint name="NotBlank"/> | ||
<constraint name="Type">string</constraint> | ||
<constraint name="Length"> | ||
<option name="min">10</option> | ||
</constraint> | ||
<constraint name="Regex"> | ||
<option name="pattern">/address-regex/</option> | ||
</constraint> | ||
<constraint name="App\Validator\Constraints\Geolocalizable"/> | ||
</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/Localization/Place.php | ||
namespace App\Localization; | ||
|
||
use App\Validator\Constraints as AcmeAssert; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
|
||
class Place | ||
{ | ||
public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
{ | ||
$metadata->addPropertyConstraint('address', new Assert\Sequentially([ | ||
new Assert\NotBlank(), | ||
new Assert\Type("string"), | ||
new Assert\Length(['min' => 10]), | ||
new Assert\Regex(self::ADDRESS_REGEX), | ||
ogizanagi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
new AcmeAssert\Geolocalizable(), | ||
])); | ||
} | ||
} | ||
|
||
Options | ||
------- | ||
|
||
``constraints`` | ||
~~~~~~~~~~~~~~~ | ||
|
||
**type**: ``array`` [:ref:`default option <validation-default-option>`] | ||
|
||
This required option is the array of validation constraints that you want | ||
to apply sequentially. | ||
|
||
.. include:: /reference/constraints/_groups-option.rst.inc | ||
|
||
.. include:: /reference/constraints/_payload-option.rst.inc |
This file contains hidden or 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 hidden or 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
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.
One minor, do you have a regex pattern which can be added? Do we really need the regex here? If yes, should we add the constant to the code example?
But adding a regex is maybe distracting, could we inline the pattern or is it anyway to complex?
What do you think of removing it completely from the example?
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.
I'd prefer to avoid adding a regex pattern. I used (but didn't show) a constant for the reason mentioned: the regex isn't important on its own and would be distracting.
I think the
@Regex
in the example deserve a purpose to demonstrate the step-by-step application of the rules, as it'll usually fail hard despite being used conjointly withType
without theSequential
constraint. But@Length
demonstrate the same, so if you really prefer, I can remove it indeed.