@@ -212,9 +212,10 @@ convenient for passwords::
212
212
Normalizing the Answer
213
213
----------------------
214
214
215
- You can normalize the answer. For instance, in a previous example you asked for
216
- the bundle name. If the user accidentally adds spaces before and/or after the name we
217
- want to remove this from the answer. You can set the normalizer using
215
+ Before validating the answer, you can "normalize" it to fix minor errors or
216
+ tweak it as needed. For instance, in a previous example you asked for the bundle
217
+ name. In case the user adds white spaces around the name by mistake, you can
218
+ trim the name before validating it. To do so, configure a normalizer using the
218
219
:method: `Symfony\\ Component\\ Console\\ Question\\ Question::setNormalizer `
219
220
method::
220
221
@@ -224,13 +225,10 @@ method::
224
225
public function execute(InputInterface $input, OutputInterface $output)
225
226
{
226
227
// ...
227
- $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
228
- $question->setNormalizer(function ($answer) {
229
- if (!is_string($value)) { // $value can be null here
230
- return $value;
231
- }
232
-
233
- return trim($value);
228
+ $question = new Question('Please enter the name of the bundle', 'AppBundle');
229
+ $question->setNormalizer(function ($value) {
230
+ // $value can be null here
231
+ return $value ? trim($value) : '';
234
232
});
235
233
236
234
$name = $helper->ask($input, $output, $question);
@@ -239,10 +237,9 @@ method::
239
237
240
238
.. caution ::
241
239
242
- The normalizer is called before the validator and the returned value by the
243
- normalizer is used as input for the validator (and not the original answer
244
- of the user). The normalizer should not throw an exception if the answer is invalid;
245
- for validation use a validator.
240
+ The normalizer is called first and the returned value is used as the input
241
+ of the validator. If the answer is invalid, don't throw exceptions in the
242
+ normalizer and let the validator handle those errors.
246
243
247
244
<
3DBF
div class="diff-text-inner">Validating the Answer
248
245
---------------------
0 commit comments