8000 bug #20141 [Console] Fix validation of empty values using SymfonyQues… · symfony/symfony@4c854e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4c854e7

Browse files
committed
bug #20141 [Console] Fix validation of empty values using SymfonyQuestionHelper::ask() (chalasr)
This PR was merged into the 2.7 branch. Discussion ---------- [Console] Fix validation of empty values using SymfonyQuestionHelper::ask() | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? |no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a When using `QuestionHelper::ask()` it's allowed to return an empty value as answer, e.g: ```php $helper = new QuestionHelper(); $question = new Question('foo', false); $question->setValidator(function ($v) { return $v; }); $answer = $helper->ask($input, $output, $question); ``` Just typing `enter` for answering this question works, the value of `$answer` would be `false`. But doing the same with `SymfonyQuestionHelper::ask()`: ```php $helper = new SymfonyQuestionHelper(); $question = new Question('foo', false); $question->setValidator(function ($v) { return $v; }); $answer = $helper->ask($input, $output, $question); ``` > [ERROR] A value is required. Same for `''` or `null`. Here I kept the same check but used as default validator, if a validator is set and allows an empty value to be returned then it's ok. Also I am not sure about if this default validator should be kept, imho we should be consistent with the default question helper, using the `SymfonyQuestionHelper` should only impact the output. Diff best viewed [like this](https://github.com/symfony/symfony/pull/20141/files?w=1) ping @kbond Commits ------- a8b910b [Console] Fix validation of null values using SymfonyStyle::ask()
2 parents 21af4f0 + a8b910b commit 4c854e7

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
3434
$question->setValidator(function ($value) use ($validator) {
3535
if (null !== $validator) {
3636
$value = $validator($value);
37-
}
38-
39-
// make required
40-
if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) {
41-
throw new \Exception('A value is required.');
37+
} else {
38+
// make required
39+
if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) {
40+
throw new \Exception('A value is required.');
41+
}
4242
}
4343

4444
return $value;

src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Console\Helper\HelperSet;
77
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
88
use Symfony\Component\Console\Output\StreamOutput;
9+
use Symfony\Component\Console\Question\Question;
910
use Symfony\Component\Console\Question\ChoiceQuestion;
1011

1112
/**
@@ -73,6 +74,15 @@ public function testAskChoice()
7374
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
7475
}
7576

77+
public function testAskReturnsNullIfValidatorAllowsIt()
78+
{
79+
$questionHelper = new SymfonyQuestionHelper();
80+
$questionHelper->setInputStream($this->getInputStream("\n"));
81+
$question = new Question('What is your favorite superhero?');
82+
$question->setValidator(function ($value) { return $value; });
83+
$this->assertNull($questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
84+
}
85+
7686
protected function getInputStream($input)
7787
{
7888
$stream = fopen('php://memory', 'r+', false);

0 commit comments

Comments
 (0)
0