Skip to content

Commit

Permalink
feature #4086 [Reference][Constraints] Added hint about attaching the…
Browse files Browse the repository at this point in the history
… expression constraint to a form field (peterrehm)

This PR was submitted for the master branch but it was merged into the 2.4 branch instead (closes #4086).

Discussion
----------

[Reference][Constraints] Added hint about attaching the expression constraint to a form field

| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | 2.4
| Fixed tickets | -

As per my discussion with @webmozart a while ago on twitter https://twitter.com/peterrehm/status/434350363940904960 I am now providing the hint about the possibility to add a expression constraint to a form field.

Commits
-------

3023dc3 Added hint about attaching the expression constraint to a form field
  • Loading branch information
weaverryan committed Aug 21, 2014
2 parents 850b93b + abc3927 commit 5379f54
Showing 1 changed file with 102 additions and 14 deletions.
116 changes: 102 additions & 14 deletions reference/constraints/Expression.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand All @@ -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;
Expand All @@ -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;
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.
Expand Down

0 comments on commit 5379f54

Please sign in to comment.