@@ -160,18 +160,30 @@ could use the ``ask`` method described above or, to make sure the user
160
160
provided a correct answer, the ``askAndValidate `` method. Both have
161
161
the disadvantage that you need to handle incorrect values yourself.
162
162
163
- Instead, you can use the ``select `` method, which makes sure that the user
164
- can only enter a valid string from a predefined list::
163
+ Instead, you can use the
164
+ :method: `Symfony\\ Component\\ Console\\ Helper\\ DialogHelper::select `
165
+ method, which makes sure that the user can only enter a valid string
166
+ from a predefined list::
165
167
166
168
$dialog = $app->getHelperSet()->get('dialog');
167
169
$colors = array('red', 'blue', 'yellow');
168
170
169
- $color = $dialog->select($output, 'Please select your favorite color (default to red)', $colors, 0);
171
+ $colorKey = $dialog->select($output, 'Please select your favorite color (default to red)', $colors, 0);
172
+ $output->writeln('You have just selected: ' . $colors[$color]);
170
173
171
- // Work with the color
174
+ // ... do something with the color
172
175
173
176
If the user enters an invalid string, an error message is shown and the user
174
177
is asked to provide the answer another time, till he enters a valid string.
175
178
176
- The last parameter is the index of the default value in the array or ``null `` if
177
- no default should be provided.
179
+ The ``select `` method takes 6 parameters:
180
+
181
+ * ``output ``: The output instance
182
+ * ``question ``: The question to ask
183
+ * ``choices ``: An array of strings with the choices the user can pick
184
+ * ``default ``: The index of the default value in the array or ``null `` if no
185
+ default should be provided (default ``null ``)
186
+ * ``attempts ``: Maximum number of times to ask or ``false `` for infinite times
187
+ (default ``false ``)
188
+ * ``errorMessage ``: Error message to display when wrong answer is entered (default
189
+ ``Value "%s" is invalid ``)
0 commit comments