-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Added option type checks in some FormTypes #12425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
7136987
to
eb7e9b4
Compare
@@ -239,7 +239,9 @@ public function setDefaultOptions(OptionsResolverInterface $resolver) | |||
)); | |||
|
|||
$resolver->setAllowedTypes(array( | |||
'choices' => array('null', 'array'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong. It also support Traversable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof, however, passing an ArrayObject
which implements Traversable
triggers an exception due to SimpleChoiceList::__construct
$choices
parameter type hint.
This is a test case that would fail against the current master
branch:
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
{
private $choices = array(
'a' => 'Bernhard',
'b' => 'Fabien',
'c' => 'Kris',
'd' => 'Jon',
'e' => 'Roman',
);
// ...
public function testChoicesOptionSupportsTraversable()
{
$this->factory->create('choice', null, array(
'choices' => new \ArrayObject($this->choices),
));
}
}
And this is how it fails:
There was 1 error:
1) Symfony\Component\Form\Tests\Extension\Core\Type\ChoiceTypeTest::testChoicesOptionSupportsTraversable
Argument 1 passed to Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList::__construct() must be of the type array, object given, called in /Users/kix/Documents/Code/php/symfony/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php on line 176 and defined
/symfony/src/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php:45
/symfony/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php:176
/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php:836
/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php:769
/symfony/src/Symfony/Component/Form/ResolvedFormType.php:109
/symfony/src/Symfony/Component/Form/FormFactory.php:87
/symfony/src/Symfony/Component/Form/FormFactory.php:67
/symfony/src/Symfony/Component/Form/FormFactory.php:39
/symfony/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php:1302
Should this become a separate issue? The doc clearly states that the a valid 'choices'
option has to be be an array
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, the choice type itself indeed does not support iterators. but child types (for instance the EntityType) are supporting it, while your change prevents it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the condition in
if (!$options['choice_list'] && !is_array($options['choices']) && !$options['choices'] instanceof \Traversable) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, should this check be implemented in the ChoiceType
at all? It looks like an OptionsResolver
's responsibility to filter out invalid values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The OptionsResolver cannot make an option required only when the other is not there.
Also, looking at this line which @stof has linked to, I think that the exception message could be more clear. If I pass a non- |
Seems like we should wait for #12148 here? |
Ping @stof @webmozart |
Once the validation for the choices option of the ChoiceType is fixed to avoid breaking BC in child types, I think this should be merged (and it should go in older branches than master) |
@kix can you update the PR to fix the case of the ChoiceType ? |
@stof, yeah, sorry for the wait, been a bit too busy by the end of the year. |
I've had to remove type hints in some |
@@ -100,7 +108,7 @@ public function __construct($choices, array $labels, array $preferredChoices = a | |||
* @param array $labels The labels belonging to the choices. | |||
* @param array $preferredChoices The choices to display with priority. | |||
*/ | |||
protected function initialize($choices, array $labels, array $preferredChoices) | |||
protected function initialize($choices, $labels, $preferredChoices) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changing this signature is a BC break
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof is right. However, this class is not an API class, so technically this change is possible, as long as the change and the upgrade path is documented in the UPGRADE file (as per our BC policy). Can you add that information to the UPGRADE file please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@webmozart, sure, will do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a note in the UPGRADE file, it looks like this is the only remaining thing to do before merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fabpot This file has been removed in 3.0.
This PR should be rebased before a new review IMHO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A new PR on master is indeed needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, will try to fix it up.
Looks good! Can you please rebase and document how to upgrade subclasses of ChoiceList in the UPGRADE file? After that, I'm 👍. |
10508cf
to
753a08e
Compare
@webmozart, does 7d623ab look good to you? Anything I should add or remove? |
@@ -110,6 +110,9 @@ UPGRADE FROM 2.x to 3.0 | |||
|
|||
### Form | |||
|
|||
* Type hints for `ChoiceList::initialize()` method's `$labels` and `$preferredChoices` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"The array type hints..."
Could you please rebase your PR onto 2.8? You need to open a new PR for the 2.8 branch as well. |
@webmozart, sorry, I must've rebased better. Will fix and write a comment once it looks good, OK? |
7d623ab
to
34e3a24
Compare
@kix This one looks easy to finish :) |
@fabpot, sure!
|
@kix Any news here? :) |
@xabbuh, sorry for the wait, I'll try to finish this by the end of the week (hopefully, if nothing unusual comes up) |
@xabbuh @kix This PR is deprecated by #14050, those changes are not needed anymore. Excepted allow type |
@HeahDude, hmm, thanks, I'll take a look at |
Yes :) |
If you do you should do it on 2.8 I think. Throw the right exception should be a bug fix IMO. |
@HeahDude, agreed, thanks :) |
Follow-up to #11696. Some of the built-in form types allowed passing invalid options, which lead to errors that were not so easy to understand. This PR adds needed checks where applicable.
Allowed types in
TimeType
'sempty_value
option were taken from the documentation.