Skip to content

Commit d65b84e

Browse files
committed
Describe validation callables
1 parent 9e971f8 commit d65b84e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

validation/callables.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.. index::
2+
single: Validation; Callables
3+
4+
How to Use Validation Constraints in Callable Validation (Validation Callables)
5+
===============================================================================
6+
7+
Sometimes you need to reuse Symfony's constraints in places like the Symfony Console,
8+
to validate the answer to a console question. ``Validation::createCallable()``
9+
can be used to create a callable based on the given constraints::
10+
11+
use Symfony\Component\Console\Style\SymfonyStyle;
12+
use Symfony\Component\Validator\Constraints\NotNull;
13+
use Symfony\Component\Validator\Validation;
14+
15+
$io = new SymfonyStyle($input, $output);
16+
$validation = Validation::createCallable(new NotBlank());
17+
18+
$wsdl = $io->ask('Wsdl location URL', null, $validation);
19+
20+
The argument of ``createCallable()`` is variadic, so you can pass any number of constraints::
21+
22+
// ...
23+
use Symfony\Component\Validator\Constraints\Length;
24+
use Symfony\Component\Validator\Constraints\Url;
25+
26+
$validation = Validation::createCallable(new Length(['max' => 255]), new Url());
27+
28+
.. versionadded:: 5.1
29+
30+
``Validation::createCallable()`` was introduced in Symfony 5.1.

0 commit comments

Comments
 (0)