Skip to content

Commit 1ebbe71

Browse files
committed
Merge pull request symfony#2589 from WouterJ/issue_2571
Documented multiselect feature
2 parents 80a0264 + 727dcf6 commit 1ebbe71

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

components/console/helpers/dialoghelper.rst

+34-4
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,48 @@ from a predefined list::
194194
$colors,
195195
0
196196
);
197-
$output->writeln('You have just selected: ' . $colors[$color]);
197+
$output->writeln('You have just selected: '.$colors[$color]);
198198
199199
// ... do something with the color
200200
201201
The option which should be selected by default is provided with the fourth
202-
parameter. The default is ``null``, which means that no option is the default one.
202+
argument. The default is ``null``, which means that no option is the default one.
203203

204204
If the user enters an invalid string, an error message is shown and the user
205205
is asked to provide the answer another time, until she enters a valid string
206206
or the maximum attempts is reached (which you can define in the fifth
207-
parameter). The default value for the attempts is ``false``, which means infinite
208-
attempts. You can define your own error message in the sixth parameter.
207+
argument). The default value for the attempts is ``false``, which means infinite
208+
attempts. You can define your own error message in the sixth argument.
209+
210+
.. versionadded:: 2.3
211+
Multiselect support was new in Symfony 2.3
212+
213+
Multiple Choices
214+
................
215+
216+
Sometimes, multiple answers can be given. The DialogHelper provides this
217+
feature using comma seperated values. This is disabled by default, to enable
218+
this set the seventh argument to ``true``::
219+
220+
// ...
221+
222+
$selected = $dialog->select(
223+
$output,
224+
'Please select your favorite color (default to red)',
225+
$colors,
226+
0,
227+
false,
228+
'Value "%s" is invalid',
229+
true // enable multiselect
230+
);
231+
232+
$selectedColors = array_map(funtion($c) use ($colors) {
233+
return $colors[$c];
234+
}, $selected)
235+
236+
$output->writeln('You have just selected: ' . implode(', ', $selectedColors));
237+
238+
Now, when the user inputted ``1,2``, the result will be: ``You have just selected: blue, yellow``
209239

210240
Testing a Command which expects input
211241
-------------------------------------

0 commit comments

Comments
 (0)