|
| 1 | +Yaml |
| 2 | +==== |
| 3 | + |
| 4 | +Validates that a value has valid `YAML`_ syntax. |
| 5 | + |
| 6 | +.. versionadded:: 7.2 |
| 7 | + |
| 8 | + The ``Yaml`` constraint was introduced in Symfony 7.2. |
| 9 | + |
| 10 | +========== =================================================================== |
| 11 | +Applies to :ref:`property or method <validation-property-target>` |
| 12 | +Class :class:`Symfony\\Component\\Validator\\Constraints\\Yaml` |
| 13 | +Validator :class:`Symfony\\Component\\Validator\\Constraints\\YamlValidator` |
| 14 | +========== =================================================================== |
| 15 | + |
| 16 | +Basic Usage |
| 17 | +----------- |
| 18 | + |
| 19 | +The ``Yaml`` constraint can be applied to a property or a "getter" method: |
| 20 | + |
| 21 | +.. configuration-block:: |
| 22 | + |
| 23 | + .. code-block:: php-attributes |
| 24 | +
|
| 25 | + // src/Entity/Report.php |
| 26 | + namespace App\Entity; |
| 27 | +
|
| 28 | + use Symfony\Component\Validator\Constraints as Assert; |
| 29 | +
|
| 30 | + class Report |
| 31 | + { |
| 32 | + #[Assert\Yaml( |
| 33 | + message: "Your configuration doesn't have valid YAML syntax." |
| 34 | + )] |
| 35 | + private string $customConfiguration; |
| 36 | + } |
| 37 | +
|
| 38 | + .. code-block:: yaml |
| 39 | +
|
| 40 | + # config/validator/validation.yaml |
| 41 | + App\Entity\Report: |
| 42 | + properties: |
| 43 | + customConfiguration: |
| 44 | + - Yaml: |
| 45 | + message: Your configuration doesn't have valid YAML syntax. |
| 46 | +
|
| 47 | + .. code-block:: xml |
| 48 | +
|
| 49 | + <!-- config/validator/validation.xml --> |
| 50 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 51 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 52 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 53 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 54 | +
|
| 55 | + <class name="App\Entity\Report"> |
| 56 | + <property name="customConfiguration"> |
| 57 | + <constraint name="Yaml"> |
| 58 | + <option name="message">Your configuration doesn't have valid YAML syntax.</option> |
| 59 | + </constraint> |
| 60 | + </property> |
| 61 | + </class> |
| 62 | + </constraint-mapping> |
| 63 | +
|
| 64 | + .. code-block:: php |
| 65 | +
|
| 66 | + // src/Entity/Report.php |
| 67 | + namespace App\Entity; |
| 68 | +
|
| 69 | + use Symfony\Component\Validator\Constraints as Assert; |
| 70 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 71 | +
|
| 72 | + class Report |
| 73 | + { |
| 74 | + public static function loadValidatorMetadata(ClassMetadata $metadata): void |
| 75 | + { |
| 76 | + $metadata->addPropertyConstraint('customConfiguration', new Assert\Yaml([ |
| 77 | + 'message' => 'Your configuration doesn\'t have valid YAML syntax.', |
| 78 | + ])); |
| 79 | + } |
| 80 | + } |
| 81 | +
|
| 82 | +Options |
| 83 | +------- |
| 84 | + |
| 85 | +``flags`` |
| 86 | +~~~~~~~~~ |
| 87 | + |
| 88 | +**type**: ``integer`` **default**: ``0`` |
| 89 | + |
| 90 | +This option enables optional features of the YAML parser when validating contents. |
| 91 | +Its value is a combination of one or more of the :ref:`flags defined by the Yaml component <yaml-flags>`: |
| 92 | + |
| 93 | +.. configuration-block:: |
| 94 | + |
| 95 | + .. code-block:: php-attributes |
| 96 | +
|
| 97 | + // src/Entity/Report.php |
| 98 | + namespace App\Entity; |
| 99 | +
|
| 100 | + use Symfony\Component\Validator\Constraints as Assert; |
| 101 | + use Symfony\Component\Yaml\Yaml; |
| 102 | +
|
| 103 | + class Report |
| 104 | + { |
| 105 | + #[Assert\Yaml( |
| 106 | + message: "Your configuration doesn't have valid YAML syntax.", |
| 107 | + flags: Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_DATETIME, |
| 108 | + )] |
| 109 | + private string $customConfiguration; |
| 110 | + } |
| 111 | +
|
| 112 | + .. code-block:: php |
| 113 | +
|
| 114 | + // src/Entity/Report.php |
| 115 | + namespace App\Entity; |
| 116 | +
|
| 117 | + use Symfony\Component\Validator\Constraints as Assert; |
| 118 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 119 | + use Symfony\Component\Yaml\Yaml; |
| 120 | +
|
| 121 | + class Report |
| 122 | + { |
| 123 | + public static function loadValidatorMetadata(ClassMetadata $metadata): void |
| 124 | + { |
| 125 | + $metadata->addPropertyConstraint('customConfiguration', new Assert\Yaml([ |
| 126 | + 'message' => 'Your configuration doesn\'t have valid YAML syntax.', |
| 127 | + 'flags' => Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_DATETIME, |
| 128 | + ])); |
| 129 | + } |
| 130 | + } |
| 131 | +
|
| 132 | +``message`` |
| 133 | +~~~~~~~~~~~ |
| 134 | + |
| 135 | +**type**: ``string`` **default**: ``This value is not valid YAML.`` |
| 136 | + |
| 137 | +This message shown if the underlying data is not a valid YAML value. |
| 138 | + |
| 139 | +You can use the following parameters in this message: |
| 140 | + |
| 141 | +=============== ============================================================== |
| 142 | +Parameter Description |
| 143 | +=============== ============================================================== |
| 144 | +``{{ error }}`` The full error message from the YAML parser |
| 145 | +``{{ line }}`` The line where the YAML syntax error happened |
| 146 | +=============== ============================================================== |
| 147 | + |
| 148 | +.. include:: /reference/constraints/_groups-option.rst.inc |
| 149 | + |
| 150 | +.. include:: /reference/constraints/_payload-option.rst.inc |
| 151 | + |
| 152 | +.. _`YAML`: https://en.wikipedia.org/wiki/YAML |
0 commit comments