Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit62dac8f

Browse files
committed
minor#20047 [Validator] Add theWordCount constraint (alexandre-daubois)
This PR was merged into the 7.2 branch.Discussion----------[Validator] Add the `WordCount` constraintRelated tosymfony/symfony#57716Commits-------e983455 [Validator] Add the `WordCount` constraint
2 parentsccff272 +e983455 commit62dac8f

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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-mappingxmlns="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+
<classname="App\Entity\BlogPostDTO">
58+
<propertyname="content">
59+
<constraintname="WordCount">
60+
<optionname="min">100</option>
61+
<optionname="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. The default value (``null``) means that the constraint uses the current
112+
user locale.
113+
114+
..include::/reference/constraints/_groups-option.rst.inc
115+
116+
``minMessage``
117+
~~~~~~~~~~~~~~
118+
119+
**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.``
120+
121+
This is the message that will be shown if the value does not contain at least
122+
the minimum number of words.
123+
124+
You can use the following parameters in this message:
125+
126+
================ ==================================================
127+
Parameter Description
128+
================ ==================================================
129+
``{{ min }}`` The minimum number of words
130+
``{{ count }}`` The actual number of words
131+
================ ==================================================
132+
133+
``maxMessage``
134+
~~~~~~~~~~~~~~
135+
136+
**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.``
137+
138+
This is the message that will be shown if the value contains more than the
139+
maximum number of words.
140+
141+
You can use the following parameters in this message:
142+
143+
================ ==================================================
144+
Parameter Description
145+
================ ==================================================
146+
``{{ max }}`` The maximum number of words
147+
``{{ count }}`` The actual number of words
148+
================ ==================================================
149+
150+
..include::/reference/constraints/_payload-option.rst.inc

‎reference/constraints/map.rst.inc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ String Constraints
3333
* :doc:`NoSuspiciousCharacters</reference/constraints/NoSuspiciousCharacters>`
3434
* :doc:`Charset</reference/constraints/Charset>`
3535
* :doc:`MacAddress</reference/constraints/MacAddress>`
36+
* :doc:`WordCount</reference/constraints/WordCount>`
3637

3738
Comparison Constraints
3839
~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp