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;
}
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;
}
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.