Skip to content

Commit 344d8ba

Browse files
committed
Reorganized the contents of the new constraint
1 parent 6a73077 commit 344d8ba

File tree

5 files changed

+131
-73
lines changed

5 files changed

+131
-73
lines changed

Diff for: bridge/twig/validation.rst

-63
This file was deleted.

Diff for: index.rst

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ Topics
6060
web_link
6161
webhook
6262
workflow
63-
twig_bridge
6463

6564
Components
6665
----------

Diff for: reference/constraints/Twig.rst

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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.

Diff for: reference/constraints/map.rst.inc

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ String Constraints
3434
* :doc:`PasswordStrength </reference/constraints/PasswordStrength>`
3535
* :doc:`Regex </reference/constraints/Regex>`
3636
* :doc:`Slug </reference/constraints/Slug>`
37+
* :doc:`Twig </reference/constraints/Twig>`
3738
* :doc:`Ulid </reference/constraints/Ulid>`
3839
* :doc:`Url </reference/constraints/Url>`
3940
* :doc:`UserPassword </reference/constraints/UserPassword>`

Diff for: twig_bridge.rst

-9
This file was deleted.

0 commit comments

Comments
 (0)