Skip to content

Commit b89e08c

Browse files
committed
Describe validation callables
1 parent a25ee5a commit b89e08c

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

components/console/helpers/questionhelper.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,22 @@ If you reach this max number it will use the default value. Using ``null`` means
397397
the amount of attempts is infinite. The user will be asked as long as they provide an
398398
invalid answer and will only be able to proceed if their input is valid.
399399

400+
.. tip::
401+
402+
You can even use the :doc:`Validator </components/validation>` component to
403+
validate the input by using the :method:`Symfony\\Component\\Validator\\Validation::createCallable`
404+
method::
405+
406+
use Symfony\Component\Validator\Constraints as Assert;
407+
use Symfony\Component\Validator\Validation;
408+
409+
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
410+
$validation = Validation::createCallable(new Assert\Regex([
411+
'pattern' => '/^[a-zA-Z]+Bundle$',
412+
'message' => 'The name of the bundle should be suffixed with \'Bundle\'',
413+
]));
414+
$question->setValidator($validation);
415+
400416
Validating a Hidden Response
401417
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
402418

components/options_resolver.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,19 @@ returns ``true`` for acceptable values and ``false`` for invalid values::
376376
// return true or false
377377
});
378378

379+
You can even use the :doc:`Validator </components/validation>` component to validate the
380+
input by using the :method:`Symfony\\Component\\Validator\\Validation::createValidCallable`
381+
method::
382+
383+
use Symfony\Component\OptionsResolver\OptionsResolver;
384+
use Symfony\Component\Validator\Constraints as Assert;
385+
use Symfony\Component\Validator\Validation;
386+
387+
// ...
388+
$resolver->setAllowedValues('transport', Validation::createValidCallable(
389+
new Assert\Length(['min' => 10 ])
390+
));
391+
379392
In sub-classes, you can use :method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::addAllowedValues`
380393
to add additional allowed values without erasing the ones already set.
381394

validation.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,30 @@ Inside the template, you can output the list of errors exactly as needed:
230230
Each validation error (called a "constraint violation"), is represented by
231231
a :class:`Symfony\\Component\\Validator\\ConstraintViolation` object.
232232

233+
.. index::
234+
single: Validation; Callables
235+
236+
Validation Callables
237+
~~~~~~~~~~~~~~~~~~~~
238+
239+
.. versionadded:: 5.1
240+
241+
``Validation::createCallable()`` was introduced in Symfony 5.1.
242+
243+
.. versionadded:: 5.3
244+
245+
``Validation::createValidCallable()`` was introduced in Symfony 5.3.
246+
247+
The ``Validation`` also allows you to create a closure to validate values
248+
against a set of constraints (e.g. in :doc:`Console </components/console/helpers/questionhelper>`
249+
or :doc:`OptionsResolver </components/options_resolver>`):
250+
251+
:method:`Symfony\\Component\\Validator\\Validation::createCallable`
252+
This returns a closure that throws ``ValidationFailedException`` when the
253+
constraints aren't matched.
254+
:method:`Symfony\\Component\\Validator\\Validation::createValidCallable`
255+
This returns a closure that returns ``false`` when the constraints aren't matched.
256+
233257
.. index::
234258
single: Validation; Constraints
235259

0 commit comments

Comments
 (0)