|
| 1 | +EqualTo |
| 2 | +======= |
| 3 | + |
| 4 | +.. versionadded:: 2.3 |
| 5 | + This constraint is new in version 2.3. |
| 6 | + |
| 7 | +Validates that a value is equal to another value, defined in the options. To |
| 8 | +force that a value is *not* equal, see :doc:`/reference/constraints/NotEqual`. |
| 9 | + |
| 10 | +.. caution:: |
| 11 | + |
| 12 | + This constraint compares using ``==``, so ``3`` and ``"3"`` are considered |
| 13 | + equal. Use :doc:`/reference/constraints/IdenticalTo` to compare with |
| 14 | + ``===``. |
| 15 | + |
| 16 | ++----------------+-----------------------------------------------------------------------+ |
| 17 | +| Applies to | :ref:`property or method<validation-property-target>` | |
| 18 | ++----------------+-----------------------------------------------------------------------+ |
| 19 | +| Options | - `value`_ | |
| 20 | +| | - `message`_ | |
| 21 | ++----------------+-----------------------------------------------------------------------+ |
| 22 | +| Class | :class:`Symfony\\Component\\Validator\\Constraints\\EqualTo` | |
| 23 | ++----------------+-----------------------------------------------------------------------+ |
| 24 | +| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\EqualToValidator` | |
| 25 | ++----------------+-----------------------------------------------------------------------+ |
| 26 | + |
| 27 | +Basic Usage |
| 28 | +----------- |
| 29 | + |
| 30 | +If you want to ensure that the ``age`` of a ``Person`` class is equal to |
| 31 | +``20``, you could do the following: |
| 32 | + |
| 33 | +.. configuration-block:: |
| 34 | + |
| 35 | + .. code-block:: yaml |
| 36 | +
|
| 37 | + # src/SocialBundle/Resources/config/validation.yml |
| 38 | + Acme\SocialBundle\Entity\Person: |
| 39 | + properties: |
| 40 | + age: |
| 41 | + - EqualTo: |
| 42 | + value: 20 |
| 43 | +
|
| 44 | + .. code-block:: php-annotations |
| 45 | +
|
| 46 | + // src/Acme/SocialBundle/Entity/Person.php |
| 47 | + namespace Acme\SocialBundle\Entity; |
| 48 | +
|
| 49 | + use Symfony\Component\Validator\Constraints as Assert; |
| 50 | +
|
| 51 | + class Person |
| 52 | + { |
| 53 | + /** |
| 54 | + * @Assert\EqualTo( |
| 55 | + * value = 20 |
| 56 | + * ) |
| 57 | + */ |
| 58 | + protected $age; |
| 59 | + } |
| 60 | +
|
| 61 | + .. code-block:: xml |
| 62 | +
|
| 63 | + <!-- src/Acme/SocialBundle/Resources/config/validation.xml --> |
| 64 | + <class name="Acme\SocialBundle\Entity\Person"> |
| 65 | + <property name="age"> |
| 66 | + <constraint name="EqualTo"> |
| 67 | + <option name="value">20</option> |
| 68 | + </constraint> |
| 69 | + </property> |
| 70 | + </class> |
| 71 | +
|
| 72 | + .. code-block:: php |
| 73 | +
|
| 74 | + // src/Acme/SocialBundle/Entity/Person.php |
| 75 | + namespace Acme\SocialBundle\Entity; |
| 76 | +
|
| 77 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 78 | + use Symfony\Component\Validator\Constraints as Assert; |
| 79 | +
|
| 80 | + class Person |
| 81 | + { |
| 82 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 83 | + { |
| 84 | + $metadata->addPropertyConstraint('age', new Assert\EqualTo(array( |
| 85 | + 'value' => 20, |
| 86 | + ))); |
| 87 | + } |
| 88 | + } |
| 89 | +
|
| 90 | +Options |
| 91 | +------- |
| 92 | + |
| 93 | +value |
| 94 | +~~~~~ |
| 95 | + |
| 96 | +**type**: ``mixed`` |
| 97 | + |
| 98 | +This option is required. It defines the value to compare to. It can be a |
| 99 | +string, number or object. |
| 100 | + |
| 101 | +message |
| 102 | +~~~~~~~ |
| 103 | + |
| 104 | +**type**: ``string`` **default**: ``This value should be equal to {{ compared_value }}`` |
| 105 | + |
| 106 | +This is the message that will be shown if the value is not equal. |
0 commit comments