|
| 1 | +WordCount |
| 2 | +========= |
| 3 | + |
| 4 | +.. versionadded:: 7.2 |
| 5 | + |
| 6 | + The ``WordCount`` constraint was introduced in Symfony 7.2. |
| 7 | + |
| 8 | +Validates that a string (or an object implementing the ``Stringable`` PHP interface) |
| 9 | +contains a given number of words. Internally, this constraint uses the |
| 10 | +:phpclass:`IntlBreakIterator` class to count the words depending on your locale. |
| 11 | + |
| 12 | +========== ======================================================================= |
| 13 | +Applies to :ref:`property or method <validation-property-target>` |
| 14 | +Class :class:`Symfony\\Component\\Validator\\Constraints\\WordCount` |
| 15 | +Validator :class:`Symfony\\Component\\Validator\\Constraints\\WordCountValidator` |
| 16 | +========== ======================================================================= |
| 17 | + |
| 18 | +Basic Usage |
| 19 | +----------- |
| 20 | + |
| 21 | +If you wanted to ensure that the ``content`` property of a ``BlogPostDTO`` |
| 22 | +class contains between 100 and 200 words, you could do the following: |
| 23 | + |
| 24 | +.. configuration-block:: |
| 25 | + |
| 26 | + .. code-block:: php-attributes |
| 27 | +
|
| 28 | + // src/Entity/BlogPostDTO.php |
| 29 | + namespace App\Entity; |
| 30 | +
|
| 31 | + use Symfony\Component\Validator\Constraints as Assert; |
| 32 | +
|
| 33 | + class BlogPostDTO |
| 34 | + { |
| 35 | + #[Assert\WordCount(min: 100, max: 200)] |
| 36 | + protected string $content; |
| 37 | + } |
| 38 | +
|
| 39 | + .. code-block:: yaml |
| 40 | +
|
| 41 | + # config/validator/validation.yaml |
| 42 | + App\Entity\BlogPostDTO: |
| 43 | + properties: |
| 44 | + content: |
| 45 | + - WordCount: |
| 46 | + min: 100 |
| 47 | + max: 200 |
| 48 | +
|
| 49 | + .. code-block:: xml |
| 50 | +
|
| 51 | + <!-- config/validator/validation.xml --> |
| 52 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 53 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 54 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 55 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 56 | +
|
| 57 | + <class name="App\Entity\BlogPostDTO"> |
| 58 | + <property name="content"> |
| 59 | + <constraint name="WordCount"> |
| 60 | + <option name="min">100</option> |
| 61 | + <option name="max">200</option> |
| 62 | + </constraint> |
| 63 | + </property> |
| 64 | + </class> |
| 65 | + </constraint-mapping> |
| 66 | +
|
| 67 | + .. code-block:: php |
| 68 | +
|
| 69 | + // src/Entity/BlogPostDTO.php |
| 70 | + namespace App\Entity; |
| 71 | +
|
| 72 | + use Symfony\Component\Validator\Constraints as Assert; |
| 73 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 74 | +
|
| 75 | + class BlogPostDTO |
| 76 | + { |
| 77 | + // ... |
| 78 | +
|
| 79 | + public static function loadValidatorMetadata(ClassMetadata $metadata): void |
| 80 | + { |
| 81 | + $metadata->addPropertyConstraint('content', new Assert\WordCount([ |
| 82 | + 'min' => 100, |
| 83 | + 'max' => 200, |
| 84 | + ])); |
| 85 | + } |
| 86 | + } |
| 87 | +
|
| 88 | +Options |
| 89 | +------- |
| 90 | + |
| 91 | +``min`` |
| 92 | +~~~~~~~ |
| 93 | + |
| 94 | +**type**: ``integer`` **default**: ``null`` |
| 95 | + |
| 96 | +The minimum number of words that the value must contain. |
| 97 | + |
| 98 | +``max`` |
| 99 | +~~~~~~~ |
| 100 | + |
| 101 | +**type**: ``integer`` **default**: ``null`` |
| 102 | + |
| 103 | +The maximum number of words that the value must contain. |
| 104 | + |
| 105 | +``locale`` |
| 106 | +~~~~~~~~~~ |
| 107 | + |
| 108 | +**type**: ``string`` **default**: ``null`` |
| 109 | + |
| 110 | +The locale to use for counting the words by using the :phpclass:`IntlBreakIterator` |
| 111 | +class. |
| 112 | + |
| 113 | +.. include:: /reference/constraints/_groups-option.rst.inc |
| 114 | + |
| 115 | +``minMessage`` |
| 116 | +~~~~~~~~~~~~~~ |
| 117 | + |
| 118 | +**type**: ``string`` **default**: ``This value is too short. It should contain at least one word.|This value is too short. It should contain at least {{ min }} words.`` |
| 119 | + |
| 120 | +This is the message that will be shown if the value does not contain at least |
| 121 | +the minimum number of words. |
| 122 | + |
| 123 | +You can use the following parameters in this message: |
| 124 | + |
| 125 | +================ ================================================== |
| 126 | +Parameter Description |
| 127 | +================ ================================================== |
| 128 | +``{{ min }}`` The minimum number of words |
| 129 | +``{{ count }}`` The actual number of words |
| 130 | +================ ================================================== |
| 131 | + |
| 132 | +``maxMessage`` |
| 133 | +~~~~~~~~~~~~~~ |
| 134 | + |
| 135 | +**type**: ``string`` **default**: ``This value is too long. It should contain one word.|This value is too long. It should contain {{ max }} words or less.`` |
| 136 | + |
| 137 | +This is the message that will be shown if the value contains more than the |
| 138 | +maximum number of words. |
| 139 | + |
| 140 | +You can use the following parameters in this message: |
| 141 | + |
| 142 | +================ ================================================== |
| 143 | +Parameter Description |
| 144 | +================ ================================================== |
| 145 | +``{{ max }}`` The maximum number of words |
| 146 | +``{{ count }}`` The actual number of words |
| 147 | +================ ================================================== |
| 148 | + |
| 149 | +.. include:: /reference/constraints/_payload-option.rst.inc |
0 commit comments