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()
This commit is contained in:
commit
4c854e7329
@ -34,12 +34,12 @@ class SymfonyQuestionHelper extends QuestionHelper
|
|||||||
$question->setValidator(function ($value) use ($validator) {
|
$question->setValidator(function ($value) use ($validator) {
|
||||||
if (null !== $validator) {
|
if (null !== $validator) {
|
||||||
$value = $validator($value);
|
$value = $validator($value);
|
||||||
}
|
} else {
|
||||||
|
|
||||||
// make required
|
// make required
|
||||||
if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) {
|
if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) {
|
||||||
throw new \Exception('A value is required.');
|
throw new \Exception('A value is required.');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
});
|
});
|
||||||
|
@ -6,6 +6,7 @@ use Symfony\Component\Console\Helper\FormatterHelper;
|
|||||||
use Symfony\Component\Console\Helper\HelperSet;
|
use Symfony\Component\Console\Helper\HelperSet;
|
||||||
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
|
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
|
||||||
use Symfony\Component\Console\Output\StreamOutput;
|
use Symfony\Component\Console\Output\StreamOutput;
|
||||||
|
use Symfony\Component\Console\Question\Question;
|
||||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -73,6 +74,15 @@ class SymfonyQuestionHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
|
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAskReturnsNullIfValidatorAllowsIt()
|
||||||
|
{
|
||||||
|
$questionHelper = new SymfonyQuestionHelper();
|
||||||
|
$questionHelper->setInputStream($this->getInputStream("\n"));
|
||||||
|
$question = new Question('What is your favorite superhero?');
|
||||||
|
$question->setValidator(function ($value) { return $value; });
|
||||||
|
$this->assertNull($questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
||||||
|
}
|
||||||
|
|
||||||
protected function getInputStream($input)
|
protected function getInputStream($input)
|
||||||
{
|
{
|
||||||
$stream = fopen('php://memory', 'r+', false);
|
$stream = fopen('php://memory', 'r+', false);
|
||||||
|
Reference in New Issue
Block a user