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

Possibly unexpected behaviour if you provide text but no value for select items #3440

Closed
oscarduignan opened this issue Mar 29, 2023 · 0 comments · Fixed by #3773
Closed
Assignees
Labels
breaking change 🐛 bug Something isn't working the way it should (including incorrect wording in documentation) consistency nunjucks select
Milestone

Comments

@oscarduignan
Copy link
Contributor

Description of the issue

possible foot gun when using govukSelect component and not passing an explicit value to your select items

https://ukgovernmentdigital.slack.com/archives/C6DMEH5R6/p1680092442731649

alphagov/govuk-design-system-backlog#60

Steps to reproduce the issue

pass only the text parameter to select items when using govukSelect

Actual vs expected behaviour

If you don't supply a value to a select option <option>text</option> then the value of that form field will be "text"

However, with govukSelect, if you don't supply a value for one of the select items then you'll get an empty value attribute <option value="">text</option> which will cause the value of that form field to (maybe unexpectedly) be an empty string ""

I can see the value of being able to pass an empty string as a value, but it seems like it should be an explicit choice rather than the default

Environment (where applicable)

N/A

  • Operating system:
  • Browser:
  • Browser version:
  • GOV.UK Frontend Version:
@oscarduignan oscarduignan added awaiting triage Needs triaging by team 🐛 bug Something isn't working the way it should (including incorrect wording in documentation) labels Mar 29, 2023
@36degrees 36degrees added this to the v5.0 milestone Jun 8, 2023
@36degrees 36degrees self-assigned this Jun 8, 2023
36degrees added a commit that referenced this issue Jun 9, 2023
In HTML, if you omit the `value` attribute from an `<option>`the value defaults to the text contained inside the element [1].

However if you omit the value from an option when using the `govukSelect` macro, we currently include an empty value attribute on the `<option>` element. This would mean that when submitting a form using a select with options with no value set, the value submitted would be the empty string.

Instead, omit the `value` attribute if no `value` option has been provided. We then mimic the default HTML behaviour, as the option value will then naturally fall back to the text content.

We still need to ensure that the `value` attribute is included when the value option is passed but has a falsey value (like an empty string, 0, or boolean false).

Fixes #3440.

[1]: https://html.spec.whatwg.org/multipage/form-elements.html#the-option-element:~:text=The%20value%20attribute%20provides%20a%20value%20for%20element.%20The%20value%20of%20an%20option%20element%20is%20the%20value%20of%20the%20value%20content%20attribute%2C%20if%20there%20is%20one%2C%20or%2C%20if%20there%20is%20not%2C%20the%20value%20of%20the%20element%27s%20text%20IDL%20attribute.
36degrees added a commit that referenced this issue Jun 9, 2023
In HTML, if you omit the `value` attribute from an `<option>`the value defaults to the text contained inside the element [1].

However if you omit the value from an option when using the `govukSelect` macro, we currently include an empty value attribute on the `<option>` element. This would mean that when submitting a form using a select with options with no value set, the value submitted would be the empty string.

Instead, omit the `value` attribute if no `value` option has been provided. We then mimic the default HTML behaviour, as the option value will then naturally fall back to the text content.

We still need to ensure that the `value` attribute is included when the value option is passed but has a falsey value (like an empty string, 0, or boolean false).

Fixes #3440.

[1]: https://html.spec.whatwg.org/multipage/form-elements.html#the-option-element:~:text=The%20value%20attribute%20provides%20a%20value%20for%20element.%20The%20value%20of%20an%20option%20element%20is%20the%20value%20of%20the%20value%20content%20attribute%2C%20if%20there%20is%20one%2C%20or%2C%20if%20there%20is%20not%2C%20the%20value%20of%20the%20element%27s%20text%20IDL%20attribute.
colinrotherham pushed a commit that referenced this issue Jul 19, 2023
In HTML, if you omit the `value` attribute from an `<option>`the value defaults to the text contained inside the element [1].

However if you omit the value from an option when using the `govukSelect` macro, we currently include an empty value attribute on the `<option>` element. This would mean that when submitting a form using a select with options with no value set, the value submitted would be the empty string.

Instead, omit the `value` attribute if no `value` option has been provided. We then mimic the default HTML behaviour, as the option value will then naturally fall back to the text content.

We still need to ensure that the `value` attribute is included when the value option is passed but has a falsey value (like an empty string, 0, or boolean false).

Fixes #3440.

[1]: https://html.spec.whatwg.org/multipage/form-elements.html#the-option-element:~:text=The%20value%20attribute%20provides%20a%20value%20for%20element.%20The%20value%20of%20an%20option%20element%20is%20the%20value%20of%20the%20value%20content%20attribute%2C%20if%20there%20is%20one%2C%20or%2C%20if%20there%20is%20not%2C%20the%20value%20of%20the%20element%27s%20text%20IDL%20attribute.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change 🐛 bug Something isn't working the way it should (including incorrect wording in documentation) consistency nunjucks select
Projects
Development

Successfully merging a pull request may close this issue.

3 participants