|
| 1 | +Charset |
| 2 | +======= |
| 3 | + |
| 4 | +.. versionadded:: 7.1 |
| 5 | + |
| 6 | + The ``Charset`` constraint was introduced in Symfony 7.1. |
| 7 | + |
| 8 | +Validates that a string is encoded in a given charset. |
| 9 | + |
| 10 | +========== ===================================================================== |
| 11 | +Applies to :ref:`property or method <validation-property-target>` |
| 12 | +Class :class:`Symfony\\Component\\Validator\\Constraints\\Charset` |
| 13 | +Validator :class:`Symfony\\Component\\Validator\\Constraints\\CharsetValidator` |
| 14 | +========== ===================================================================== |
| 15 | + |
| 16 | +Basic Usage |
| 17 | +----------- |
| 18 | + |
| 19 | +If you wanted to ensure that the ``content`` property of an ``FileDTO`` |
| 20 | +class uses UTF-8, you could do the following: |
| 21 | + |
| 22 | +.. configuration-block:: |
| 23 | + |
| 24 | + .. code-block:: php-attributes |
| 25 | +
|
| 26 | + // src/Entity/FileDTO.php |
| 27 | + namespace App\Entity; |
| 28 | +
|
| 29 | + use Symfony\Component\Validator\Constraints as Assert; |
| 30 | +
|
| 31 | + class FileDTO |
| 32 | + { |
| 33 | + #[Assert\Charset('UTF-8')] |
| 34 | + protected string $content; |
| 35 | + } |
| 36 | +
|
| 37 | + .. code-block:: yaml |
| 38 | +
|
| 39 | + # config/validator/validation.yaml |
| 40 | + App\Entity\FileDTO: |
| 41 | + properties: |
| 42 | + content: |
| 43 | + - Charset: 'UTF-8' |
| 44 | +
|
| 45 | + .. code-block:: xml |
| 46 | +
|
| 47 | + <!-- config/validator/validation.xml --> |
| 48 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 49 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 50 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 51 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 52 | +
|
| 53 | + <class name="App\Entity\FileDTO"> |
| 54 | + <property name="content"> |
| 55 | + <constraint name="Charset"> |
| 56 | + <option name="charset">UTF-8</option> |
| 57 | + </constraint> |
| 58 | + </property> |
| 59 | + </class> |
| 60 | + </constraint-mapping> |
| 61 | +
|
| 62 | + .. code-block:: php |
| 63 | +
|
| 64 | + // src/Entity/FileDTO.php |
| 65 | + namespace App\Entity; |
| 66 | +
|
| 67 | + use Symfony\Component\Validator\Constraints as Assert; |
| 68 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 69 | +
|
| 70 | + class FileDTO |
| 71 | + { |
| 72 | + // ... |
| 73 | +
|
| 74 | + public static function loadValidatorMetadata(ClassMetadata $metadata): void |
| 75 | + { |
| 76 | + $metadata->addPropertyConstraint('content', new Assert\Charset('UTF-8')); |
| 77 | + } |
| 78 | + } |
| 79 | +
|
| 80 | +Options |
| 81 | +------- |
| 82 | + |
| 83 | +``encodings`` |
| 84 | +~~~~~~~~~~~~~ |
| 85 | + |
| 86 | +**type**: ``array`` | ``string`` **default**: ``[]`` |
| 87 | + |
| 88 | +An encoding or a set of encodings to check against. If you pass an array of |
| 89 | +encodings, the validator will check if the value is encoded in *any* of the |
| 90 | +encodings. This option accept any value that can be passed to |
| 91 | +:phpfunction:`mb_detect_encoding()`. |
| 92 | + |
| 93 | +.. include:: /reference/constraints/_groups-option.rst.inc |
| 94 | + |
| 95 | +``message`` |
| 96 | +~~~~~~~~~~~ |
| 97 | + |
| 98 | +**type**: ``string`` **default**: ``The detected encoding "{{ detected }}" does not match one of the accepted encoding: "{{ encodings }}".`` |
| 99 | + |
| 100 | +This is the message that will be shown if the value does not match any of the |
| 101 | +accepted encodings. |
| 102 | + |
| 103 | +You can use the following parameters in this message: |
| 104 | + |
| 105 | +=================== ============================================================== |
| 106 | +Parameter Description |
| 107 | +=================== ============================================================== |
| 108 | +``{{ detected }}`` The detected encoding |
| 109 | +``{{ encodings }}`` The accepted encodings |
| 110 | +=================== ============================================================== |
| 111 | + |
| 112 | +.. include:: /reference/constraints/_payload-option.rst.inc |
0 commit comments