Skip to content

[DomCrawler] check for the correct field type #11692

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

Merged
merged 1 commit into from
Aug 21, 2014
Merged

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Aug 18, 2014

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

HTML allow to define different form fields with the same name. Imagine the following form:

    <html>
        <body>
            <form action="/">
                <input type="hidden" name="option" value="default">
                <input type="radio" name="option" value="A">
                <input type="radio" name="option" value="B">
                <input type="hidden" name="settings[1]" value="0">
                <input type="checkbox" name="settings[1]" value="1" id="setting-1">
                <button>klickme</button>
            </form>
        </body>
    </html>

Since the FormFieldRegistry can only handle one field per name, the hidden field option is registered first before the radio field with the same name is evaluated. Thus, the FormFieldRegistry returns an InputFormField instance on which the addChoices() method can not be called.

// there may be other fields with the same name that are no choice
// fields already registered
// see https://github.com/symfony/symfony/issues/11689
if ($this->get($node->getAttribute('name')) instanceof ChoiceFormField) {
Copy link
Member

Choose a reason for hiding this comment

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

for consistency with other cases, this condition should be moved inside the previous if, so that the ChoiceFormField is added in other cases, for consistency with other cases where the same name appears several times

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you sure that this doesn't lead to any unexpected behaviour?

Copy link
Member

Choose a reason for hiding this comment

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

well, if you have 2 text inputs with the same name, the second one wins. If you have a radio button and then a select with the same name, the second one (the select) wins. But in your code, if you have a field followed by a radio with the same name, the first field will win. This is inconsistent. we should always make the field overwrite the previous field for consistency (which also means that in the case of the form shown in your PR description, the radio button will be available rather than the hidden input)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, makes sense.

@stof
Copy link
Member

stof commented Aug 18, 2014

👍 (the test failure is a volatile test in the Process component)

HTML allow to define different form fields with the same name.
Imagine the following form:

    <html>
        <body>
            <form action="/">
                <input type="hidden" name="option" value="default">
                <input type="radio" name="option" value="A">
                <input type="radio" name="option" value="B">
                <input type="hidden" name="settings[1]" value="0">
                <input type="checkbox" name="settings[1]" value="1" id="setting-1">
                <button>klickme</button>
            </form>
        </body>
    </html>

Since the `FormFieldRegistry` can only handle one field per name, the
hidden field option is registered first before the radio field with
the same name is evaluated. Thus, the `FormFieldRegistry` returns an
`InputFormField` instance on which the `addChoices()` method can not
be called.
@romainneutron
Copy link
Contributor

👍

@stof
Copy link
Member

stof commented Aug 21, 2014

Thanks for fixing this bug @xabbuh.

@stof stof merged commit 169b397 into symfony:2.3 Aug 21, 2014
stof added a commit that referenced this pull request Aug 21, 2014
This PR was merged into the 2.3 branch.

Discussion
----------

[DomCrawler] check for the correct field type

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

HTML allow to define different form fields with the same name. Imagine the following form:

```html
    <html>
        <body>
            <form action="/">
                <input type="hidden" name="option" value="default">
                <input type="radio" name="option" value="A">
                <input type="radio" name="option" value="B">
                <input type="hidden" name="settings[1]" value="0">
                <input type="checkbox" name="settings[1]" value="1" id="setting-1">
                <button>klickme</button>
            </form>
        </body>
    </html>
```

Since the `FormFieldRegistry` can only handle one field per name, the hidden field option is registered first before the radio field with the same name is evaluated. Thus, the `FormFieldRegistry` returns an `InputFormField` instance on which the `addChoices()` method can not be called.

Commits
-------

169b397 check for the correct field type
@xabbuh xabbuh deleted the issue-11689 branch August 21, 2014 22:00
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.

3 participants