Skip to content

[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
wants to merge 1 commit 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
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>

Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

.. code-block:: php

// src/Acme/DemoBundle/Model/BlogPost.php
namespace Acme\DemoBundle\Model;

use Symfony\Component\Validator\Constraints as Assert;
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
Expand Down