8000 [2.0] Add paragraph about ask a validated response in the console component by romainneutron · Pull Request #1893 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[2.0] Add paragraph about ask a validated response in the console component #1893

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
Nov 10, 2012
Merged
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
Add paragraph about ask a validated response
  • Loading branch information
romainneutron committed Nov 6, 2012
commit c91362dc79a45c235e19b6c8660a1a6023cead92
31 changes: 31 additions & 0 deletions components/console/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,37 @@ if you needed to know the name of something, you might do the following::
'foo'
);

Ask and validate response
-------------------------

You can easily ask question and validate response with built-in methods::

$dialog = $this->getHelperSet()->get('dialog');

$validator = function ($value) {
if ('' === trim($value)) {
throw new \Exception('The value can not be empty');
}

return $value;
}

$password = $dialog->askAndValidate(
$output,
'Please enter the name of the widget',
$validator,
20,
'foo'
);

The validation callback can be any callable PHP function, the fourth argument is
the maximum number of attempts, set it to ``false`` for unlimited attempts. The
fifth argument is the default value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should mention that the validator should throw an exception to reject the value


The callback must throw an exception in case the value is not acceptable. Please
note that the callback **must** return the value. The value can be modified by
the callback (it will be returned modified by the helper).

Testing Commands
430B ----------------

Expand Down
0