Skip to content

Commit 2d05c9e

Browse files
committed
Add doc for Yaml's option collection.
1 parent 04ccd92 commit 2d05c9e

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

reference/constraints/Yaml.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,57 @@ Parameter Description
145145
``{{ line }}`` The line where the YAML syntax error happened
146146
=============== ==============================================================
147147

148+
``collection``
149+
~~~~~~~~~~~~~~~
150+
151+
**type**: :class:``Symfony\Component\Validator\Constraints`` **default**: ``null``
152+
153+
This option apply a :class:``Symfony\Component\Validator\Constraints\Collection`` constraint to Yaml parsed data.
154+
You can apply validation on the content of your Yaml.
155+
156+
.. configuration-block::
157+
158+
.. code-block:: php-attributes
159+
160+
// src/Entity/Report.php
161+
namespace App\Entity;
162+
163+
use App\Yaml\MyConfiguration;
164+
use Symfony\Component\Validator\Constraints as Assert;
165+
166+
class Report
167+
{
168+
#[Assert\Yaml(
169+
collection: new Assert\Collection([
170+
'foo' => new Assert\NotNull(),
171+
]),
172+
)]
173+
private string $customConfiguration;
174+
}
175+
176+
.. code-block:: php
177+
178+
// src/Entity/Report.php
179+
namespace App\Entity;
180+
181+
use App\Yaml\MyConfiguration;
182+
use Symfony\Component\Validator\Constraints as Assert;
183+
use Symfony\Component\Validator\Mapping\ClassMetadata;
184+
185+
class Report
186+
{
187+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
188+
{
189+
$metadata->addPropertyConstraint('customConfiguration', new Assert\Yaml([
190+
collection: new Assert\Collection([
191+
'foo' => new Assert\NotNull(),
192+
]),
193+
]));
194+
}
195+
}
196+
197+
See :doc:`Collection constraint </reference/constraints/Collection>` for more info.
198+
148199
.. include:: /reference/constraints/_groups-option.rst.inc
149200

150201
.. include:: /reference/constraints/_payload-option.rst.inc

0 commit comments

Comments
 (0)