-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Reference][Constraints] Added hint about attaching the expression constraint to a form field #4086
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
Closed
Closed
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,7 @@ One way to accomplish this is with the Expression constraint: | |
|
||
// src/Acme/DemoBundle/Model/BlogPost.php | ||
namespace Acme\DemoBundle\Model\BlogPost; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
/** | ||
|
@@ -91,23 +91,27 @@ One way to accomplish this is with the Expression constraint: | |
.. code-block:: xml | ||
|
||
<!-- src/Acme/DemoBundle/Resources/config/validation.xml --> | ||
<class name="Acme\DemoBundle\Model\BlogPost"> | ||
<constraint name="Expression"> | ||
<option name="expression"> | ||
this.getCategory() in ['php', 'symfony'] or !this.isTechnicalPost() | ||
</option> | ||
<option name="message"> | ||
If this is a tech post, the category should be either php or symfony! | ||
</option> | ||
</constraint> | ||
</class> | ||
|
||
<?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="Acme\DemoBundle\Model\BlogPost"> | ||
<constraint name="Expression"> | ||
<option name="expression"> | ||
this.getCategory() in ['php', 'symfony'] or !this.isTechnicalPost() | ||
</option> | ||
<option name="message"> | ||
If this is a tech post, the category should be either php or symfony! | ||
</option> | ||
</constraint> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/Acme/DemoBundle/Model/BlogPost.php | ||
namespace Acme\DemoBundle\Model\BlogPost; | ||
namespace Acme\DemoBundle\Model; | ||
|
||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
|
@@ -129,6 +133,90 @@ expression that must return true in order for validation to pass. To learn | |
more about the expression language syntax, see | ||
:doc:`/components/expression_language/syntax`. | ||
|
||
.. sidebar:: Mapping the Error to a Specific Field | ||
|
||
You can also attach the constraint to a specific property and still validate | ||
based on the values of the entire entity. This is handy if you want to attach | ||
the error to a specific field. In this context, ``value`` represents the value | ||
of ``isTechnicalPost``. | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# src/Acme/DemoBundle/Resources/config/validation.yml | ||
Acme\DemoBundle\Model\BlogPost: | ||
properties: | ||
isTechnicalPost: | ||
- Expression: | ||
expression: "this.getCategory() in ['php', 'symfony'] or value == false" | ||
message: "If this is a tech post, the category should be either php or symfony!" | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/Acme/DemoBundle/Model/BlogPost.php | ||
namespace Acme\DemoBundle\Model; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class BlogPost | ||
{ | ||
// ... | ||
|
||
/** | ||
* @Assert\Expression( | ||
* "this.getCategory() in ['php', 'symfony'] or value == false", | ||
* message="If this is a tech post, the category should be either php or symfony!" | ||
* ) | ||
*/ | ||
private $isTechnicalPost; | ||
|
||
// ... | ||
} | ||
|
||
.. code-block:: xml | ||
|
||
<!-- src/Acme/DemoBundle/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="Acme\DemoBundle\Model\BlogPost"> | ||
<property name="isTechnicalPost"> | ||
<constraint name="Expression"> | ||
<option name="expression"> | ||
this.getCategory() in ['php', 'symfony'] or value == false | ||
</option> | ||
<option name="message"> | ||
If this is a tech post, the category should be either php or symfony! | ||
</option> | ||
</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/Acme/DemoBundle/Model/BlogPost.php | ||
namespace Acme\DemoBundle\Model; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
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 sort the use statements alphabetically. But we have no rule for this. So, the choice is up to you. 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. Updated |
||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
|
||
class BlogPost | ||
{ | ||
public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
{ | ||
$metadata->addPropertyConstraint('isTechnicalPost', new Assert\Expression(array( | ||
'expression' => 'this.getCategory() in ["php", "symfony"] or value == false', | ||
'message' => 'If this is a tech post, the category should be either php or symfony!', | ||
))); | ||
} | ||
|
||
// ... | ||
} | ||
|
||
For more information about the expression and what variables are available | ||
to you, see the :ref:`expression <reference-constraint-expression-option>` | ||
option details below. | ||
|
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.
remove one the blank lines
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.
Updated