Skip to content

Commit db364d5

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: Added some references Describe validation callables
2 parents 5254ca6 + 57ab7ef commit db364d5

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
@@ -344,6 +344,8 @@ method::
344344
of the validator. If the answer is invalid, don't throw exceptions in the
345345
normalizer and let the validator handle those errors.
346346

347+
.. _console-validate-question-answer:
348+
347349
Validating the Answer
348350
---------------------
349351

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

393+
.. tip::
394+
395+
You can even use the :doc:`Validator </validation>` component to
396+
validate the input by using the :method:`Symfony\\Component\\Validator\\Validation::createCallable`
397+
method::
398+
399+
use Symfony\Component\Validator\Constraints\Regex;
400+
use Symfony\Component\Validator\Validation;
401+
402+
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
403+
$validation = Validation::createCallable(new Regex([
404+
'pattern' => '/^[a-zA-Z]+Bundle$',
405+
'message' => 'The name of the bundle should be suffixed with \'Bundle\'',
406+
]));
407+
$question->setValidator($validation);
408+
391409
Validating a Hidden Response
392410
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
393411

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)