|
| 1 | +Twig Constraint |
| 2 | +=============== |
| 3 | + |
| 4 | +.. versionadded:: 7.3 |
| 5 | + |
| 6 | + The ``Twig`` constraint was introduced in Symfony 7.3. |
| 7 | + |
| 8 | +Validates that a given string contains valid :ref:`Twig syntax <twig-language>`. |
| 9 | +This is particularly useful when template content is user-generated or |
| 10 | +configurable, and you want to ensure it can be rendered by the Twig engine. |
| 11 | + |
| 12 | +.. note:: |
| 13 | + |
| 14 | + Using this constraint requires having the ``symfony/twig-bridge`` package |
| 15 | + installed in your application (e.g. by running ``composer require symfony/twig-bridge``). |
| 16 | + |
| 17 | +========== =================================================================== |
| 18 | +Applies to :ref:`property or method <validation-property-target>` |
| 19 | +Class :class:`Symfony\\Bridge\\Twig\\Validator\\Constraints\\Twig` |
| 20 | +Validator :class:`Symfony\\Bridge\\Twig\\Validator\\Constraints\\TwigValidator` |
| 21 | +========== =================================================================== |
| 22 | + |
| 23 | +Basic Usage |
| 24 | +----------- |
| 25 | + |
| 26 | +Apply the ``Twig`` constraint to validate the contents of any property or the |
| 27 | +returned value of any method: |
| 28 | + |
| 29 | + use Symfony\Bridge\Twig\Validator\Constraints\Twig; |
| 30 | + |
| 31 | + class Template |
| 32 | + { |
| 33 | + #[Twig] |
| 34 | + private string $templateCode; |
| 35 | + } |
| 36 | + |
| 37 | +.. configuration-block:: |
| 38 | + |
| 39 | + .. code-block:: php-attributes |
| 40 | +
|
| 41 | + // src/Entity/Page.php |
| 42 | + namespace App\Entity; |
| 43 | +
|
| 44 | + use Symfony\Bridge\Twig\Validator\Constraints\Twig; |
| 45 | +
|
| 46 | + class Page |
| 47 | + { |
| 48 | + #[Twig] |
| 49 | + private string $templateCode; |
| 50 | + } |
| 51 | +
|
| 52 | + .. code-block:: yaml |
| 53 | +
|
| 54 | + # config/validator/validation.yaml |
| 55 | + App\Entity\Page: |
| 56 | + properties: |
| 57 | + templateCode: |
| 58 | + - Symfony\Bridge\Twig\Validator\Constraints\Twig: ~ |
| 59 | +
|
| 60 | + .. code-block:: xml |
| 61 | +
|
| 62 | + <!-- config/validator/validation.xml --> |
| 63 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 64 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 65 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 66 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 67 | +
|
| 68 | + <class name="App\Entity\Page"> |
| 69 | + <property name="templateCode"> |
| 70 | + <constraint name="Symfony\Bridge\Twig\Validator\Constraints\Twig"/> |
| 71 | + </property> |
| 72 | + </class> |
| 73 | + </constraint-mapping> |
| 74 | +
|
| 75 | + .. code-block:: php |
| 76 | +
|
| 77 | + // src/Entity/Page.php |
| 78 | + namespace App\Entity; |
| 79 | +
|
| 80 | + use Symfony\Bridge\Twig\Validator\Constraints\Twig; |
| 81 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 82 | +
|
| 83 | + class Page |
| 84 | + { |
| 85 | + // ... |
| 86 | +
|
| 87 | + public static function loadValidatorMetadata(ClassMetadata $metadata): void |
| 88 | + { |
| 89 | + $metadata->addPropertyConstraint('templateCode', new Twig()); |
| 90 | + } |
| 91 | + } |
| 92 | +
|
| 93 | +Constraint Options |
| 94 | +------------------ |
| 95 | + |
| 96 | +``message`` |
| 97 | +~~~~~~~~~~~ |
| 98 | + |
| 99 | +**type**: ``message`` **default**: ``This value is not a valid Twig template.`` |
| 100 | + |
| 101 | +This is the message displayed when the given string does *not* contain valid Twig syntax:: |
| 102 | + |
| 103 | + // ... |
| 104 | + |
| 105 | + class Page |
| 106 | + { |
| 107 | + #[Twig(message: 'Check this Twig code; it contains errors.')] |
| 108 | + private string $templateCode; |
| 109 | + } |
| 110 | + |
| 111 | +This message has no parameters. |
| 112 | + |
| 113 | +``skipDeprecations`` |
| 114 | +~~~~~~~~~~~~~~~~~~~~ |
| 115 | + |
| 116 | +**type**: ``boolean`` **default**: ``true`` |
| 117 | + |
| 118 | +If ``true``, Twig deprecation warnings are ignored during validation. Set it to |
| 119 | +``false`` to trigger validation errors when the given Twig code contains any deprecations:: |
| 120 | + |
| 121 | + // ... |
| 122 | + |
| 123 | + class Page |
| 124 | + { |
| 125 | + #[Twig(skipDeprecations: false)] |
| 126 | + private string $templateCode; |
| 127 | + } |
| 128 | + |
| 129 | +This can be helpful when enforcing stricter template rules or preparing for major |
| 130 | +Twig version upgrades. |
0 commit comments