33
44Validates that a value is a valid locale.
55
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 the ` ICU format locale IDs `_. For example,
7+ the two letter ` ISO 639-1 `_ * language * code (e.g. ``fr ``), or the language code
8+ followed by an underscore (`` _ ``) and the `ISO 3166-1 alpha-2 `_ *country * code
9+ (e.g. `` fr_FR `` for French/France).
1010
1111+----------------+------------------------------------------------------------------------+
1212| Applies to | :ref: `property or method <validation-property-target >` |
1313+----------------+------------------------------------------------------------------------+
1414| Options | - `message `_ |
1515| | - `payload `_ |
16+ | | - `canonicalize `_ |
1617+----------------+------------------------------------------------------------------------+
1718| Class | :class: `Symfony\\ Component\\ Validator\\ Constraints\\ Locale ` |
1819+----------------+------------------------------------------------------------------------+
@@ -34,7 +35,9 @@ Basic Usage
3435 class User
3536 {
3637 /**
37- * @Assert\Locale()
38+ * @Assert\Locale(
39+ * canonicalize = true
40+ * )
3841 */
3942 protected $locale;
4043 }
@@ -45,7 +48,8 @@ Basic Usage
4548 App\Entity\User :
4649 properties :
4750 locale :
48- - Locale : ~
51+ - Locale :
52+ canonicalize : true
4953
5054 .. code-block :: xml
5155
@@ -57,7 +61,9 @@ Basic Usage
5761
5862 <class name =" App\Entity\User" >
5963 <property name =" locale" >
60- <constraint name =" Locale" />
64+ <constraint name =" Locale" >
65+ <option name =" canonicalize" >true</option >
66+ </constraint >
6167 </property >
6268 </class >
6369 </constraint-mapping >
@@ -74,7 +80,7 @@ Basic Usage
7480 {
7581 public static function loadValidatorMetadata(ClassMetadata $metadata)
7682 {
77- $metadata->addPropertyConstraint('locale', new Assert\Locale());
83+ $metadata->addPropertyConstraint('locale', new Assert\Locale(array('canonicalize' => true) ));
7884 }
7985 }
8086
@@ -90,5 +96,19 @@ This message is shown if the string is not a valid locale.
9096
9197.. include :: /reference/constraints/_payload-option.rst.inc
9298
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+
112+ .. _`ICU format locale IDs` : http://userguide.icu-project.org/locale
93113.. _`ISO 639-1` : https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
94114.. _`ISO 3166-1 alpha-2` : https://en.wikipedia.org/wiki/ISO_3166-1#Current_codes
0 commit comments