@@ -212,9 +212,10 @@ convenient for passwords::
212212Normalizing the Answer
213213----------------------
214214
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
218219:method: `Symfony\\ Component\\ Console\\ Question\\ Question::setNormalizer `
219220method::
220221
@@ -224,13 +225,10 @@ method::
224225 public function execute(InputInterface $input, OutputInterface $output)
225226 {
226227 // ...
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) : '';
234232 });
235233
236234 $name = $helper->ask($input, $output, $question);
@@ -239,10 +237,9 @@ method::
239237
240238.. caution ::
241239
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.
246243
247244Validating the Answer
248245---------------------
0 commit comments