8000 Merge pull request #1893 from romainneutron/ConsoleDialog · symfony/symfony-docs@4a11852 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a11852

Browse files
committed
Merge pull request #1893 from romainneutron/ConsoleDialog
[2.0] Add paragraph about ask a validated response in the console component
2 parents a5458f6 + c91362d commit 4a11852

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

components/console/introduction.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,37 @@ if you needed to know the name of something, you might do the following::
287287
'foo'
288288
);
289289

290+
Ask and validate response
291+
-------------------------
292+
293+
You can easily ask question and validate response with built-in methods::
294+
295+
$dialog = $this->getHelperSet()->get('dialog');
296+
297+
$validator = function ($value) {
298+
if ('' === trim($value)) {
299+
throw new \Exception('The value can not be empty');
300+
}
301+
302+
return $value;
303+
}
304+
305+
$password = $dialog->askAndValidate(
306+
$output,
307+
'Please enter the name of the widget',
308+
$validator,
309+
20,
310+
'foo'
311+
);
312+
313+
The validation callback can be any callable PHP function, the fourth argument is
314+
the maximum number of attempts, set it to ``false`` for unlimited attempts. The
315+
fifth argument is the default value.
316+
317+
The callback must throw an exception in case the value is not acceptable. Please
318+
note that the callback **must** return the value. The value can be modified by
319+
the callback (it will be returned modified by the helper).
320+
290321
Testing Commands
291322
----------------
292323

0 commit comments

Comments
 (0)
0