3
3
4
4
Validates that a value is a valid locale.
5
5
6
- The "value" for each locale is either the two letter ` ISO 639-1 `_ * language *
7
- code (e.g. ``fr ``), or the language code followed by an underscore (`` _ ``),
8
- then the `ISO 3166-1 alpha-2 `_ *country * code (e.g. ``fr_FR `` for
9
- French/France).
6
+ The "value" for each locale is any of ` ICU format locale IDs `. By instance the two
7
+ letter ` ISO 639-1 `_ * language * code (e.g. ``fr ``), or the language code followed
8
+ by an underscore (`` _ ``), then the `ISO 3166-1 alpha-2 `_ *country * code (e.g. ``fr_FR ``
9
+ for French/France).
10
10
11
11
+----------------+------------------------------------------------------------------------+
12
12
| Applies to | :ref: `property or method <validation-property-target >` |
13
13
+----------------+------------------------------------------------------------------------+
14
14
| Options | - `message `_ |
15
15
| | - `payload `_ |
16
+ | | - `canonicalize `_ |
16
17
+----------------+------------------------------------------------------------------------+
17
18
| Class | :class: `Symfony\\ Component\\ Validator\\ Constraints\\ Locale ` |
18
19
+----------------+------------------------------------------------------------------------+
@@ -34,7 +35,9 @@ Basic Usage
34
35
class User
35
36
{
36
37
/**
37
- * @Assert\Locale()
38
+ * @Assert\Locale(
39
+ * canonicalize = true
40
+ * )
38
41
*/
39
42
protected $locale;
40
43
}
@@ -45,7 +48,8 @@ Basic Usage
45
48
App\Entity\User :
46
49
properties :
47
50
locale :
48
- - Locale : ~
51
+ - Locale :
52
+ canonicalize : true
49
53
50
54
.. code-block :: xml
51
55
@@ -57,7 +61,9 @@ Basic Usage
57
61
58
62
<class name =" App\Entity\User" >
59
63
<property name =" locale" >
60
- <constraint name =" Locale" />
64
+ <constraint name =" Locale" >
65
+ <option name =" canonicalize" >true</option >
66
+ </constraint >
61
67
</property >
62
68
</class >
63
69
</constraint-mapping >
@@ -74,7 +80,7 @@ Basic Usage
74
80
{
75
81
public static function loadValidatorMetadata(ClassMetadata $metadata)
76
82
{
77
- $metadata->addPropertyConstraint('locale', new Assert\Locale());
83
+ $metadata->addPropertyConstraint('locale', new Assert\Locale(array('canonicalize' => true) ));
78
84
}
79
85
}
80
86
@@ -90,5 +96,19 @@ This message is shown if the string is not a valid locale.
90
96
91
97
.. include :: /reference/constraints/_payload-option.rst.inc
92
98
99
+ canonicalize
100
+ ~~~~~~~~~~~~
101
+
102
+ **type **: ``boolean `` **default **: ``false ``
103
+
104
+ .. versionadded :: 4.1
105
+ Using this option with value ``false `` was deprecated in Symfony 4.1 and it
106
+ will throw an exception in Symfony 5.0, use `true ` instead.
107
+
108
+ If true, the :phpmethod: `Locale::canonicalize ` method will be applied before checking
109
+ the validity of the given locale (e.g. ``FR-fr.utf8 `` is transformed into ``fr_FR ``
110
+ ).
111
+
93
112
.. _`ISO 639-1` : https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
94
113
.. _`ISO 3166-1 alpha-2` : https://en.wikipedia.org/wiki/ISO_3166-1#Current_codes
114
+ .. _`ICU format locale IDs` : http://userguide.icu-project.org/locale#TOC-Canonicalization
0 commit comments