Warning: You are browsing the documentation for Symfony 2.2, which is no longer maintained.

Read the updated version of this page for Symfony 5.3 (the current stable version).

Dialog Helper

Dialog Helper

The Symfony\Component\Console\Helper\DialogHelper provides functions to ask the user for more information. It is included in the default helper set, which you can get by calling getHelperSet():

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

All the methods inside the Dialog Helper have an Symfony\Component\Console\Output\OutputInterface as first the argument, the question as the second argument and the default value as last argument.

Asking the User for confirmation

Suppose you want to confirm an action before actually executing it. Add the following to your command:

// ...
if (!$dialog->askConfirmation(
        '<question>Continue with this action?</question>',
    )) {

In this case, the user will be asked “Continue with this action?”, and will return true if the user answers with y or false if the user answers with n. The third argument to askConfirmation() is the default value to return if the user doesn’t enter any input. Any other input will ask the same question again.

Asking the User for Information

You can also ask question with more than a simple yes/no answer. For instance, if you want to know a bundle name, you can add this to your command:

// ...
$bundle = $dialog->ask(
    'Please enter the name of the bundle',

The user will be asked “Please enter the name of the bundle”. They can type some name which will be returned by the ask method. If they leave it empty, the default value (AcmeDemoBundle here) is returned.

Hiding the User’s Response

New in version 2.2: The askHiddenResponse method was added in Symfony 2.2.

You can also ask a question and hide the response. This is particularly convenient for passwords:

$dialog = $this->getHelperSet()->get('dialog');
$password = $dialog->askHiddenResponse(
    'What is the database password?',


When you ask for a hidden response, Symfony will use either a binary, change stty mode or use another trick to hide the response. If none is available, it will fallback and allow the response to be visible unless you pass false as the third argument like in the example above. In this case, a RuntimeException would be thrown.

Validating the Answer

You can even validate the answer. For instance, in the last example you asked for the bundle name. Following the Symfony2 naming conventions, it should be suffixed with Bundle. You can validate that by using the askAndValidate() method:

// ...
$bundle = $dialog->askAndValidate(
    'Please enter the name of the bundle',
    function ($answer) {
        if ('Bundle' !== substr($answer, -6)) {
            throw new \RunTimeException(
                'The name of the bundle should be suffixed with \'Bundle\''
        return $answer;

This methods has 2 new arguments, the full signature is:

    OutputInterface $output,
    string|array $question,
    callback $validator,
    integer $attempts = false,
    string $default = null

The $validator is a callback which handles the validation. It should throw an exception if there is something wrong. The exception message is displayed in the console, so it is a good practice to put some useful information in it. The callback function should also return the value of the user’s input if the validation was successful.

You can set the max number of times to ask in the $attempts argument. If you reach this max number it will use the default value, which is given in the last argument. Using false means the amount of attempts is infinite. The user will be asked as long as they provide an invalid answer and will only be able to proceed if their input is valid.

Validating a Hidden Response

New in version 2.2: The askHiddenResponseAndValidate method was added in Symfony 2.2.

You can also ask and validate a hidden response:

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

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

$password = $dialog->askHiddenResponseAndValidate(
    'Please enter your password',

If you want to allow the response to be visible if it cannot be hidden for some reason, pass true as the fifth argument.

Let the user choose from a list of Answers

New in version 2.2: The select() method was added in Symfony 2.2.

If you have a predefined set of answers the user can choose from, you could use the ask method described above or, to make sure the user provided a correct answer, the askAndValidate method. Both have the disadvantage that you need to handle incorrect values yourself.

Instead, you can use the select() method, which makes sure that the user can only enter a valid string from a predefined list:

$dialog = $app->getHelperSet()->get('dialog');
$colors = array('red', 'blue', 'yellow');

$color = $dialog->select(
    'Please select your favorite color (default to red)',
$output->writeln('You have just selected: ' . $colors[$color]);

// ... do something with the color

The option which should be selected by default is provided with the fourth parameter. The default is null, which means that no option is the default one.

If the user enters an invalid string, an error message is shown and the user is asked to provide the answer another time, until they enter a valid string or the maximum attempts is reached (which you can define in the fifth parameter). The default value for the attempts is false, which means infinite attempts. You can define your own error message in the sixth parameter.

Testing a Command which expects input

If you want to write a unit test for a command which expects some kind of input from the command line, you need to overwrite the HelperSet used by the command:

use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Helper\HelperSet;

// ...
public function testExecute()
    // ...
    $commandTester = new CommandTester($command);

    $dialog = $command->getHelper('dialog');
    // Equals to a user inputing "Test" and hitting ENTER
    // If you need to enter a confirmation, "yes\n" will work

    $commandTester->execute(array('command' => $command->getName()));

    // $this->assertRegExp('/.../', $commandTester->getDisplay());

protected function getInputStream($input)
    $stream = fopen('php://memory', 'r+', false);
    fputs($stream, $input);

    return $stream;

By setting the inputStream of the DialogHelper, you imitate what the console would do internally with all user input through the cli. This way you can test any user interaction (even complex ones) by passing an appropriate input stream.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.