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