Skip to content

[Validator] Add Validation::createCallable() #15600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions components/console/helpers/questionhelper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,22 @@ If you reach this max number it will use the default value. Using ``null`` means
the amount of attempts is infinite. The user will be asked as long as they provide an
invalid answer and will only be able to proceed if their input is valid.

.. tip::

You can even use the :doc:`Validator </validation>` component to
validate the input by using the :method:`Symfony\\Component\\Validator\\Validation::createCallable`
method::

use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Validation;

$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
$validation = Validation::createCallable(new Regex([
'pattern' => '/^[a-zA-Z]+Bundle$',
'message' => 'The name of the bundle should be suffixed with \'Bundle\'',
]));
$question->setValidator($validation);

Validating a Hidden Response
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
15 changes: 15 additions & 0 deletions components/options_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,21 @@ returns ``true`` for acceptable values and ``false`` for invalid values::
// return true or false
});

.. tip::

You can even use the :doc:`Validator </validation>` component to validate the
input by using the :method:`Symfony\\Component\\Validator\\Validation::createValidCallable`
method::

use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Validation;

// ...
$resolver->setAllowedValues('transport', Validation::createValidCallable(
new Length(['min' => 10 ])
));

In sub-classes, you can use :method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::addAllowedValues`
to add additional allowed values without erasing the ones already set.

Expand Down
24 changes: 24 additions & 0 deletions validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,30 @@ Inside the template, you can output the list of errors exactly as needed:
Each validation error (called a "constraint violation"), is represented by
a :class:`Symfony\\Component\\Validator\\ConstraintViolation` object.

.. index::
single: Validation; Callables

Validation Callables
~~~~~~~~~~~~~~~~~~~~

.. versionadded:: 5.1

``Validation::createCallable()`` was introduced in Symfony 5.1.

.. versionadded:: 5.3

``Validation::createValidCallable()`` was introduced in Symfony 5.3.

The ``Validation`` also allows you to create a closure to validate values
against a set of constraints (e.g. in :doc:`Console </components/console/helpers/questionhelper>`
or :doc:`OptionsResolver </components/options_resolver>`):

:method:`Symfony\\Component\\Validator\\Validation::createCallable`
This returns a closure that throws ``ValidationFailedException`` when the
constraints aren't matched.
:method:`Symfony\\Component\\Validator\\Validation::createValidCallable`
This returns a closure that returns ``false`` when the constraints aren't matched.

.. index::
single: Validation; Constraints

Expand Down