Skip to content

Commit

Permalink
Merge branch '2.4' into 2.5
Browse files Browse the repository at this point in the history
* 2.4:
  Added hint about attaching the expression constraint to a form field
  add missing Monolog handler type in XML config
  • Loading branch information
weaverryan committed Aug 21, 2014
2 parents cd07f5f + 281d490 commit e7a17ff
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 14 deletions.
1 change: 1 addition & 0 deletions cookbook/logging/monolog_email.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ it is broken down.
/>
<monolog:handler
name="swift"
type="swift_mailer"
from-email="error@example.com"
to-email="error@example.com"
subject="An Error Occurred!"
Expand Down
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 e7a17ff

Please sign in to comment.