Xml
Validates that a value has valid XML syntax. Optionally, it can also validate the content against an XSD schema.
| Applies to | property or method |
| Class | Xml |
| Validator | XmlValidator |
8.1
The Xml constraint was introduced in Symfony 8.1.
Basic Usage
The Xml constraint can be applied to a property or a getter method:
1 2 3 4 5 6 7 8 9 10
// src/Entity/Report.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Report
{
#[Assert\Xml]
private string $xmlContent;
}
Validating Against an XSD Schema
You can validate that the XML content conforms to a specific XSD schema using
the schema option:
1 2 3 4 5 6 7 8 9 10
// src/Entity/Report.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Report
{
#[Assert\Xml(schema: 'path/to/schema.xsd')]
private string $xmlContent;
}
Options
message
type: string default: This value is not valid XML.
This message is shown if the underlying data is not valid XML.
You can use the following parameters in this message:
| Parameter | Description |
|---|---|
{{ error }} |
The full error message from the XML parser |
{{ line }} |
The line where the XML syntax error occurred |
schema
type: string default: null
The path to the XSD schema file to validate the XML content against. When this option is set, the constraint validates not only that the content is well-formed XML, but also that it conforms to the specified schema.
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.