Opened 10 years ago
Closed 9 years ago
#31909 closed enhancement (fixed)
wp_dropdown_categories() select field cannot accept HTML 'required'
Reported by: | wzislam | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 4.6 | Priority: | normal |
Severity: | normal | Version: | 4.1.1 |
Component: | Taxonomy | Keywords: | needs-patch good-first-bug |
Focuses: | template | Cc: |
Description
Steps to Reproduce
- Add a
<form>
somewhere in your theme/plugin - Call
<?php wp_dropdown_categories(); ?>
- Add a
<button>Submit</submit>
button - Close the
</form>
- Submit the form
There are no supporting argument that can pass a required option to the select field.
You can't make the select field mandatory without JavaScripts. But HTML5 has a nice required attribute that supports most of the modern browsers.
We solved the issue in this WPSE thread:
http://wordpress.stackexchange.com/q/183292/22728
But IMHO it's time to have core patch.
Attachments (3)
Change History (17)
#2
@
10 years ago
Multiple selection (<select multiple>
) was one of the issue close to this is pending till today: #16734
#5
in reply to:
↑ 1
@
9 years ago
Replying to boonebgorges:
Are there attributes other than 'required' that one might reasonably want to add to the
<select>
element? (Like maybe'data-'
attributes of some sort?) Maybe we could add aselect_atts
param, which would accept an array of additional attributes:
Yes, so far its the best idea and I usually implement it this way. instead of creating new key for required
lets just have an attrs
key or attributes
key which accepts and array of key value pairs which later become the select element attributes.
This idea can also be used on other select function throughout WP core.
#6
in reply to:
↑ 1
@
9 years ago
Replying to boonebgorges:
Are there attributes other than 'required' that one might reasonably want to add to the
<select>
element? (Like maybe'data-'
attributes of some sort?) Maybe we could add aselect_atts
param, which would accept an array of additional attributes:
I think disabled
and readonly
should also be there.
This ticket was mentioned in Slack in #core by boone. View the logs.
9 years ago
#9
@
9 years ago
- Keywords needs-patch good-first-bug added; has-patch dev-feedback removed
- Milestone changed from Awaiting Review to 4.6
In a Slack discussion (see logs linked above), @helen and I decided that this ticket is probably not the appropriate place to open the can of worms that is the-way-that-WP-generates-form-markup. A generic 'atts' parameter is too big a hammer for the nail in this ticket. If someone feels strongly that an 'atts' approach should be adopted widely in WP, please open a separate ticket with a proposal.
For the time being, let's go with a new required
parameter, along the lines of required-wp_dropdown_categories.31909.diff. That patch should be updated with the following:
@since
documentation in the docblock- There is no real reason for us to officially support integer values. Let's just call it
bool
. - A unit test showing that it works
This ticket was mentioned in Slack in #core-comments by boone. View the logs.
9 years ago
#11
@
9 years ago
Hi everyone!
this is my first patch to wp, so let me know there's something wrong.
i've followed @boonebgorges suggestions and:
- added @since 4.6.0 Added required attribute
- updated the documentation for this field
- added the unit test function to Tests_Category with the following results:
[x] test get all category ids
[x] test get category by slug
[x] test make cat compat
[x] test get cat name
[x] test get cat ID
[x] test get category by path
[x] test wp dropdown categories value field should default to term id
[x] test wp dropdown categories value field should contain required
[x] test wp dropdown categories value field term id
[x] test wp dropdown categories value field slug
[x] test wp dropdown categories value field should fall back on term id when an invalid value is provided
[x] test wp dropdown categories selected should respect custom value field
[x] test wp dropdown categories show option all should be selected if no selected value is explicitly passed and value field does not have string values
[x] test wp dropdown categories show option all should be selected if selected value of 0 string is explicitly passed and value field does not have string values
Other attributes
there's other allowed attributes, namely;
- disabled : Specifies that the element represents a disabled control
- multiple : indicates that its select element represents a control for selecting zero or more options from a list of options.
- size : The number of options to show to the user.
- form : The value of the id attribute on the form with which to associate the element.
- autofocus : Specifies that the element represents a control to which a UA is meant to give focus as soon as the document is loaded.
https://www.w3.org/TR/html-markup/select.html
(html5) https://dev.w3.org/html5/spec-author-view/the-select-element.html#the-select-element
Are there attributes other than 'required' that one might reasonably want to add to the
<select>
element? (Like maybe'data-'
attributes of some sort?) Maybe we could add aselect_atts
param, which would accept an array of additional attributes: