Yaml
Validates that a value has valid YAML syntax.
7.2
The Yaml
constraint was introduced in Symfony 7.2.
Applies to | property or method |
Class | Yaml |
Validator | YamlValidator |
Basic Usage
The Yaml
constraint can be applied to a property or a "getter" method:
1 2 3 4 5 6 7 8 9 10 11 12
// src/Entity/Report.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Report
{
#[Assert\Yaml(
message: "Your configuration doesn't have valid YAML syntax."
)]
private string $customConfiguration;
}
1 2 3 4 5 6
# config/validator/validation.yaml
App\Entity\Report:
properties:
customConfiguration:
- Yaml:
message: Your configuration doesn't have valid YAML syntax.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<!-- config/validator/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="App\Entity\Report">
<property name="customConfiguration">
<constraint name="Yaml">
<option name="message">Your configuration doesn't have valid YAML syntax.</option>
</constraint>
</property>
</class>
</constraint-mapping>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// src/Entity/Report.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;
class Report
{
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('customConfiguration', new Assert\Yaml([
'message' => 'Your configuration doesn\'t have valid YAML syntax.',
]));
}
}
Options
flags
type: integer
default: 0
This option enables optional features of the YAML parser when validating contents. Its value is a combination of one or more of the flags defined by the Yaml component:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// src/Entity/Report.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Yaml\Yaml;
class Report
{
#[Assert\Yaml(
message: "Your configuration doesn't have valid YAML syntax.",
flags: Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_DATETIME,
)]
private string $customConfiguration;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// src/Entity/Report.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Yaml\Yaml;
class Report
{
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('customConfiguration', new Assert\Yaml([
'message' => 'Your configuration doesn\'t have valid YAML syntax.',
'flags' => Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_DATETIME,
]));
}
}
message
type: string
default: This value is not valid YAML.
This message shown if the underlying data is not a valid YAML value.
You can use the following parameters in this message:
Parameter | Description |
---|---|
{{ error }} |
The full error message from the YAML parser |
{{ line }} |
The line where the YAML syntax error happened |
groups
type: array
| string
default: null
It defines the validation group or groups of this constraint. Read more about validation groups.
payload
type: mixed
default: null
This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to you.
For example, you may want to use several error levels to present failed constraints differently in the front-end depending on the severity of the error.