Skip to content
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

[DomCrawler] Throw an exception if a form field path is incomplete #14618

Merged
merged 1 commit into from May 20, 2015

Conversation

jakzal
Copy link
Contributor

@jakzal jakzal commented May 12, 2015

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

@@ -124,13 +124,15 @@ public function has($name)
public function set($name, $value)
{
$target = &$this->get($name);
if (!is_array($value) || $target instanceof Field\ChoiceFormField) {
if ($target instanceof Field\FormField) {
Copy link
Member

Choose a reason for hiding this comment

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

Will this even work when $value is an array and $target is no ChoiceFormField?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I didn't change this behaviour. For example, the following is allowed:

// for a field named foo[bar][baz]
$registry->set('foo[bar]', ['baz' => 'fbb']);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Btw, notice I have changed ChoiceFormField to FormField in the condition.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that's why I'm asking. :) Previously, if the field was no ChoiceFormField and the input was an array, the code that would have been executed was:

$fields = self::create($name, $value);
foreach ($fields->all() as $k => $v) {
    $this->set($k, $v);
}

Now, it will simply be $target->setValue($value).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@xabbuh there was an assumption that if the value is not an array, the $target is always an object. ChoiceFormField is the only implementation of FormField that works with arrays, therefore the special condition was there. FormFieldRegistry::create() expects a string as its first argument, and it would never worked with any of the FormField classes.

I think how conditions are currently organised makes it easier to understand.

Copy link
Member

Choose a reason for hiding this comment

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

@jakzal I mean something like the following:

$registry = new FormFieldRegistry();
$registry->add($this->getFormFieldMock('bar'));
$registry->set('bar', array('baz'));

Before, you got an error message like InvalidArgumentException: Unreachable field "0". Now, the value will be implicitly cast to string. Is that really the desired behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it's cast to string, it simply sets the array as a value. I'll add this test case. Cheers!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, it is cast to string ;)

public function testFormRegistrySetValueOnCompoundField()
{
$registry = new FormFieldRegistry();
$registry->add($fbb = $this->getFormFieldMock('foo[bar][baz]'));
Copy link
Member

Choose a reason for hiding this comment

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

$fbb is never used.

@fabpot
Copy link
Member

fabpot commented May 20, 2015

Thank you @jakzal.

@fabpot fabpot merged commit 991e65c into symfony:2.3 May 20, 2015
fabpot added a commit that referenced this pull request May 20, 2015
…complete (jakzal)

This PR was merged into the 2.3 branch.

Discussion
----------

[DomCrawler] Throw an exception if a form field path is incomplete

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

Commits
-------

991e65c [DomCrawler] Throw an exception if a form field path is incomplete.
@jakzal jakzal deleted the bugfix/domcrawler-11807 branch May 20, 2015 08:23
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.

None yet

3 participants