Skip to content

Xml

Edit this page

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 schemaPath 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(schemaPath: 'path/to/schema.xsd')]
    private string $xmlContent;
}

Options

formatMessage

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
{{ value }} The current (invalid) value

groups

type: array | string default: null

It defines the validation group or groups of this constraint. Read more about validation groups.

maxSize

type: integer default: 5242880 (5 MiB)

The maximum size, in bytes, of the XML content. If the value is larger than this, validation fails with the tooLargeMessage.

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.

schemaFlags

type: integer default: 0

A bitmask of libxml constants (e.g. LIBXML_SCHEMA_CREATE) that enables optional features of the XML parser when validating the content against the schema defined in the schemaPath option.

schemaMessage

type: string default: This value does not conform to the expected XSD schema.

This message is shown if the underlying data doesn't conform to the XSD schema defined in the schemaPath option.

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 error occurred

schemaPath

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.

tooLargeMessage

type: string default: This XML payload is too large ({{ size }} bytes): it exceeds the limit of {{ limit }} bytes.

This message is shown if the size of the underlying data is larger than the value of the maxSize option.

You can use the following parameters in this message:

Parameter Description
{{ size }} The size of the value (in bytes)
{{ limit }} The maximum allowed size (in bytes)
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version