|
| 1 | +.. index:: |
| 2 | + single: Console Helpers; Dialog Helper |
| 3 | + |
| 4 | +Dialog Helper |
| 5 | +============= |
| 6 | + |
| 7 | +The Dialog Helper provides functions to ask the user for more information. |
| 8 | +It is included in the default helper set, which you can get |
| 9 | +by calling :method:`Symfony\\Component\\Console\\Command\\Command::getHelperSet`:: |
| 10 | + |
| 11 | + $dialog = $this->getHelperSet()->get('dialog'); |
| 12 | + |
| 13 | +All the methods inside the Dialog Helper have an |
| 14 | +:class:`Symfony\\Component\\Console\\Output\\OutputInterface` as first argument, |
| 15 | +the question as second argument and the default value as last argument. |
| 16 | + |
| 17 | +Asking the User for confirmation |
| 18 | +-------------------------------- |
| 19 | + |
| 20 | +Suppose you want to confirm an action before actually executing it. Add |
| 21 | +the following to your command:: |
| 22 | + |
| 23 | + // ... |
| 24 | + if (!$dialog->askConfirmation( |
| 25 | + $output, |
| 26 | + '<question>Continue with this action?</question>', |
| 27 | + false |
| 28 | + )) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | +In this case, the user will be asked "Continue with this action", and will return |
| 33 | +``true`` if the user answers with ``y`` or false in any other case. The third |
| 34 | +argument to ``askConfirmation`` is the default value to return if the user doesn't |
| 35 | +enter any input. |
| 36 | + |
| 37 | +Asking the User for information |
| 38 | +------------------------------- |
| 39 | + |
| 40 | +You can also ask question with more than a simple yes/no answer. For instance, |
| 41 | +if you want to know a bundle name, you can add this to your command:: |
| 42 | + |
| 43 | + // ... |
| 44 | + $bundle = $dialog->ask( |
| 45 | + $output, |
| 46 | + 'Please enter the name of the bundle', |
| 47 | + 'AcmeDemoBundle' |
| 48 | + ); |
| 49 | + |
| 50 | +The user will be asked "Please enter the name of the bundle". They can type |
| 51 | +some name which will be returned by the ``ask`` method. If they leave it empty |
| 52 | +the default value (``AcmeDemoBundle`` here) is returned. |
| 53 | + |
| 54 | +Validating the answer |
| 55 | +--------------------- |
| 56 | + |
| 57 | +You can even validate the answer. For instance, in our last example we asked |
| 58 | +for the bundle name. Following the Symfony2 naming conventions, it should |
| 59 | +be suffixed with ``Bundle``. We can validate that by using the |
| 60 | +:method:`Symfony\\Component\\Console\\Helper\\DialogHelper::askAndValidate` |
| 61 | +method:: |
| 62 | + |
| 63 | + // ... |
| 64 | + $bundle = $dialog->askAndValidate( |
| 65 | + $output, |
| 66 | + 'Please enter the name of the bundle', |
| 67 | + function ($answer) { |
| 68 | + if ('Bundle' !== substr($answer, -6)) { |
| 69 | + throw new \RunTimeException( |
| 70 | + 'The name of the bundle should be suffixed with \'Bundle\'' |
| 71 | + ); |
| 72 | + } |
| 73 | + }, |
| 74 | + false, |
| 75 | + 'AcmeDemoBundle' |
| 76 | + ); |
| 77 | + |
| 78 | +This methods has 2 new arguments, the full signature is:: |
| 79 | + |
| 80 | + askAndValidate( |
| 81 | + OutputInterface $output, |
| 82 | + string|array $question, |
| 83 | + callback $validator, |
| 84 | + integer $attempts = false, |
| 85 | + string $default = null |
| 86 | + ) |
| 87 | + |
| 88 | +The ``$validator`` is a callback which handles the validation. It should |
| 89 | +throw an exception if there is something wrong. The exception message is displayed |
| 90 | +in the console, so it is a good practice to put some usefull information |
| 91 | +in it. |
| 92 | + |
| 93 | +You can set the max number of times to ask in the ``$attempts`` argument. |
| 94 | +If we reach this max number it will use the default value, which is given |
| 95 | +in the last argument. Using ``false`` means the amount of attempts is infinite. |
| 96 | +The user will be asked as long as he provides an invalid answer and will only |
| 97 | +be able to proceed if his input is valid. |
0 commit comments