Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Closed
wants to merge 1 commit into from

Conversation

kix
Copy link
Contributor

@kix kix commented Nov 6, 2014

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets none
License MIT
Doc PR none

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's empty_value option were taken from the documentation.

@kix kix changed the title Form option type checks [Form] Added option type checks in some FormTypes Nov 6, 2014
@kix kix force-pushed the form_option_type_checks branch 2 times, most recently from 7136987 to eb7e9b4 Compare November 6, 2014 19:12
@@ -239,7 +239,9 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
));

$resolver->setAllowedTypes(array(
'choices' => array('null', 'array'),
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Member

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) {
let me think that iterators were meant to be supported, and that this is a bug in the SimpleChoiceList conversion

Copy link
Contributor Author

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.

Copy link
Member

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.

@kix
Copy link
Contributor Author

kix commented Nov 7, 2014

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-\Traversable and non-array argument inside 'choices' option, I'd get an exception thrown that says I'm not passing anything, while I'm passing bad values.

@kix
Copy link
Contributor Author

kix commented Nov 27, 2014

Seems like we should wait for #12148 here?

@fabpot fabpot added the Form label Dec 7, 2014
@kix
Copy link
Contributor Author

kix commented Dec 12, 2014

Ping @stof @webmozart
This is referenced in symfony-docs#1226, that's 7th page of issues. Looks like a 500+100 :)

@stof
Copy link
Member

stof commented Jan 4, 2015

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)

@stof
Copy link
Member

stof commented Jan 18, 2015

@kix can you update the PR to fix the case of the ChoiceType ?

@kix
Copy link
Contributor Author

kix commented Jan 18, 2015

@stof, yeah, sorry for the wait, been a bit too busy by the end of the year.

@kix
Copy link
Contributor Author

kix commented Jan 19, 2015

I've had to remove type hints in some ChoiceList's for the test suite to pass. The check is implemented in ChoiceList::__construct() instead. Is this wrong?

@@ -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)
Copy link
Member

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

Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@webmozart, sure, will do.

Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

@webmozart
Copy link
Contributor

Looks good! Can you please rebase and document how to upgrade subclasses of ChoiceList in the UPGRADE file? After that, I'm 👍.

@kix kix force-pushed the form_option_type_checks branch from 10508cf to 753a08e Compare June 17, 2015 18:19
@kix
Copy link
Contributor Author

kix commented Jun 17, 2015

@webmozart, does 7d623ab look good to you? Anything I should add or remove?
On build failure: investigating.

@@ -110,6 +110,9 @@ UPGRADE FROM 2.x to 3.0

### Form

* Type hints for `ChoiceList::initialize()` method's `$labels` and `$preferredChoices`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The array type hints..."

@webmozart
Copy link
Contributor

Could you please rebase your PR onto 2.8? You need to open a new PR for the 2.8 branch as well.

@kix
Copy link
Contributor Author

kix commented Jun 18, 2015

@webmozart, sorry, I must've rebased better. Will fix and write a comment once it looks good, OK?

@kix kix force-pushed the form_option_type_checks branch from 7d623ab to 34e3a24 Compare January 22, 2016 00:18
@kix kix changed the title [Form] Added option type checks in some FormTypes [Form][3.0] Added option type checks in some FormTypes Jan 22, 2016
@kix kix changed the title [Form][3.0] Added option type checks in some FormTypes [Form] Added option type checks in some FormTypes Jan 22, 2016
@fabpot
Copy link
Member

fabpot commented Mar 2, 2016

@kix This one looks easy to finish :)

@kix
Copy link
Contributor Author

kix commented Mar 2, 2016

@fabpot, sure!
2 марта 2016 г. 8:36 PM пользователь "Fabien Potencier" <
[email protected]> написал:

@kix https://github.com/kix This ones looks easy to finish :)


Reply to this email directly or view it on GitHub
#12425 (comment).

@xabbuh
Copy link
Member

xabbuh commented Mar 29, 2016

@kix Any news here? :)

@kix
Copy link
Contributor Author

kix commented Mar 29, 2016

@xabbuh, sorry for the wait, I'll try to finish this by the end of the week (hopefully, if nothing unusual comes up)

@HeahDude
Copy link
Contributor

@xabbuh @kix This PR is deprecated by #14050, those changes are not needed anymore.

Excepted allow type array for entry_options which might be relevant today. Since it's normalized, it will actually make php crash on the call to $value['block_name'], but it's common sense IHMO that entry_options option in CollectionType is an array, that's probably why no one complained until now.

@kix
Copy link
Contributor Author

kix commented Mar 29, 2016

@HeahDude, hmm, thanks, I'll take a look at $value['block_name'], I think. Is it OK to close this PR?

@HeahDude
Copy link
Contributor

Yes :)

@kix kix closed this Mar 29, 2016
@HeahDude
Copy link
Contributor

If you do you should do it on 2.8 I think. Throw the right exception should be a bug fix IMO.

@kix kix deleted the form_option_type_checks branch March 29, 2016 13:37
@kix
Copy link
Contributor Author

kix commented Mar 29, 2016

@HeahDude, agreed, thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants