Skip to content

Commit 57ab7ef

Browse files
committed
Merge branch '5.3' into 5.4
* 5.3: Added some references Describe validation callables
2 parents 19f7228 + 4405199 commit 57ab7ef

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

components/console/helpers/questionhelper.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ method::
353353
of the validator. If the answer is invalid, don't throw exceptions in the
354354
normalizer and let the validator handle those errors.
355355

356+
.. _console-validate-question-answer:
357+
356358
Validating the Answer
357359
---------------------
358360

@@ -397,6 +399,22 @@ If you reach this max number it will use the default value. Using ``null`` means
397399
the amount of attempts is infinite. The user will be asked as long as they provide an
398400
invalid answer and will only be able to proceed if their input is valid.
399401

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

components/options_resolver.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ is thrown::
335335
In sub-classes, you can use :method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::addAllowedTypes`
336336
to add additional allowed types without erasing the ones already set.
337337

338+
.. _optionsresolver-validate-value:
339+
338340
Value Validation
339341
~~~~~~~~~~~~~~~~
340342

@@ -376,6 +378,21 @@ returns ``true`` for acceptable values and ``false`` for invalid values::
376378
// return true or false
377379
});
378380

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

validation.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,31 @@ 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+
The ``Validation`` also allows you to create a closure to validate values
240+
against a set of constraints (useful for example when
241+
:ref:`validating Console command answers <console-validate-question-answer>` or
242+
when :ref:`validating OptionsResolver values <optionsresolver-validate-value>`):
243+
244+
:method:`Symfony\\Component\\Validator\\Validation::createCallable`
245+
This returns a closure that throws ``ValidationFailedException`` when the
246+
constraints aren't matched.
247+
:method:`Symfony\\Component\\Validator\\Validation::createValidCallable`
248+
This returns a closure that returns ``false`` when the constraints aren't matched.
249+
250+
.. versionadded:: 5.1
251+
252+
``Validation::createCallable()`` was introduced in Symfony 5.1.
253+
254+
.. versionadded:: 5.3
255+
256+
``Validation::createValidCallable()`` was introduced in Symfony 5.3.
257+
233258
.. index::
234259
single: Validation; Constraints
235260

0 commit comments

Comments
 (0)