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
Conversation
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) { |
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.
Will this even work when $value
is an array and $target
is no ChoiceFormField
?
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.
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']);
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.
Btw, notice I have changed ChoiceFormField
to FormField
in the condition.
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.
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)
.
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.
@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.
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.
@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?
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.
I don't think it's cast to string, it simply sets the array as a value. I'll add this test case. Cheers!
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.
You're right, it is cast to string ;)
public function testFormRegistrySetValueOnCompoundField() | ||
{ | ||
$registry = new FormFieldRegistry(); | ||
$registry->add($fbb = $this->getFormFieldMock('foo[bar][baz]')); |
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.
$fbb
is never used.
3d3f4e3
to
513d736
Compare
513d736
to
991e65c
Compare
Thank you @jakzal. |
…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.