|
1 | 1 | False |
2 | 2 | ===== |
3 | 3 |
|
4 | | -Validates that a value is ``false``. Specifically, this checks to see if |
5 | | -the value is exactly ``false``, exactly the integer ``0``, or exactly the |
6 | | -string "``0``". |
7 | | - |
8 | | -Also see :doc:`True <True>`. |
9 | | - |
10 | | -+----------------+---------------------------------------------------------------------+ |
11 | | -| Applies to | :ref:`property or method <validation-property-target>` | |
12 | | -+----------------+---------------------------------------------------------------------+ |
13 | | -| Options | - `message`_ | |
14 | | -| | - `payload`_ | |
15 | | -+----------------+---------------------------------------------------------------------+ |
16 | | -| Class | :class:`Symfony\\Component\\Validator\\Constraints\\False` | |
17 | | -+----------------+---------------------------------------------------------------------+ |
18 | | -| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\FalseValidator` | |
19 | | -+----------------+---------------------------------------------------------------------+ |
20 | | - |
21 | | -Basic Usage |
22 | | ------------ |
23 | | - |
24 | | -The ``False`` constraint can be applied to a property or a "getter" method, |
25 | | -but is most commonly useful in the latter case. For example, suppose that |
26 | | -you want to guarantee that some ``state`` property is *not* in a dynamic |
27 | | -``invalidStates`` array. First, you'd create a "getter" method:: |
28 | | - |
29 | | - protected $state; |
30 | | - |
31 | | - protected $invalidStates = array(); |
32 | | - |
33 | | - public function isStateInvalid() |
34 | | - { |
35 | | - return in_array($this->state, $this->invalidStates); |
36 | | - } |
37 | | - |
38 | | -In this case, the underlying object is only valid if the ``isStateInvalid`` |
39 | | -method returns **false**: |
40 | | - |
41 | | -.. configuration-block:: |
42 | | - |
43 | | - .. code-block:: php-annotations |
44 | | -
|
45 | | - // src/AppBundle/Entity/Author.php |
46 | | - namespace AppBundle\Entity; |
47 | | -
|
48 | | - use Symfony\Component\Validator\Constraints as Assert; |
49 | | -
|
50 | | - class Author |
51 | | - { |
52 | | - /** |
53 | | - * @Assert\False( |
54 | | - * message = "You've entered an invalid state." |
55 | | - * ) |
56 | | - */ |
57 | | - public function isStateInvalid() |
58 | | - { |
59 | | - // ... |
60 | | - } |
61 | | - } |
62 | | -
|
63 | | - .. code-block:: yaml |
64 | | -
|
65 | | - # src/AppBundle/Resources/config/validation.yml |
66 | | - AppBundle\Entity\Author |
67 | | - getters: |
68 | | - stateInvalid: |
69 | | - - 'False': |
70 | | - message: You've entered an invalid state. |
71 | | -
|
72 | | - .. code-block:: xml |
73 | | -
|
74 | | - <!-- src/AppBundle/Resources/config/validation.xml --> |
75 | | - <?xml version="1.0" encoding="UTF-8" ?> |
76 | | - <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
77 | | - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
78 | | - xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
79 | | -
|
80 | | - <class name="AppBundle\Entity\Author"> |
81 | | - <getter property="stateInvalid"> |
82 | | - <constraint name="False"> |
83 | | - <option name="message">You've entered an invalid state.</option> |
84 | | - </constraint> |
85 | | - </getter> |
86 | | - </class> |
87 | | - </constraint-mapping> |
88 | | -
|
89 | | - .. code-block:: php |
90 | | -
|
91 | | - // src/AppBundle/Entity/Author.php |
92 | | - namespace AppBundle\Entity; |
93 | | -
|
94 | | - use Symfony\Component\Validator\Mapping\ClassMetadata; |
95 | | - use Symfony\Component\Validator\Constraints as Assert; |
96 | | -
|
97 | | - class Author |
98 | | - { |
99 | | - public static function loadValidatorMetadata(ClassMetadata $metadata) |
100 | | - { |
101 | | - $metadata->addGetterConstraint('stateInvalid', new Assert\False()); |
102 | | - } |
103 | | - } |
104 | | -
|
105 | 4 | .. caution:: |
106 | 5 |
|
107 | | - When using YAML, be sure to surround ``False`` with quotes (``'False'``) |
108 | | - or else YAML will convert this into a ``false`` boolean value. |
109 | | - |
110 | | -Options |
111 | | -------- |
112 | | - |
113 | | -message |
114 | | -~~~~~~~ |
115 | | - |
116 | | -**type**: ``string`` **default**: ``This value should be false.`` |
117 | | - |
118 | | -This message is shown if the underlying data is not false. |
119 | | - |
120 | | -.. include:: /reference/constraints/_payload-option.rst.inc |
| 6 | + The ``False`` constraint is deprecated since Symfony 2.7 |
| 7 | + and will be removed in Symfony 3.0. Use the |
| 8 | + :doc:`/reference/constraints/IsFalse` constraint instead. |
0 commit comments