WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 3 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:
PR Number:

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)

required-wp_dropdown_categories.31909.diff (2.3 KB) - added by wzislam 5 years ago.
I added a patch with HTML5 required attribute
wp_dropdown_categories-required.31909-4.diff (3.8 KB) - added by pcarvalho 3 years ago.
adds required attribute and a test for it
wp_dropdown_categories-required.31909-5.diff (3.8 KB) - added by pcarvalho 3 years ago.
fixed the inline documentation for the test

Download all attachments as: .zip

Change History (17)

#1 follow-ups: @boonebgorges
5 years ago

  • Keywords needs-patch added

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 a select_atts param, which would accept an array of additional attributes:

wp_dropdown_categories( array(
    // ...
    'select_atts' => array(
        'required',
        'data-foo' => 'bar',
    ),
    // ...
) );

@wzislam
5 years ago

I added a patch with HTML5 required attribute

#2 @wzislam
5 years ago

Multiple selection (<select multiple>) was one of the issue close to this is pending till today: #16734

Last edited 4 years ago by SergeyBiryukov (previous) (diff)

#3 @prionkor
4 years ago

  • Keywords has-patch dev-feedback added; needs-patch removed

#4 @SergeyBiryukov
4 years ago

  • Component changed from General to Taxonomy

#5 in reply to: ↑ 1 @prionkor
4 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 a select_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.

Last edited 4 years ago by prionkor (previous) (diff)

#6 in reply to: ↑ 1 @butterflymedia
4 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 a select_atts param, which would accept an array of additional attributes:

I think disabled and readonly should also be there.

#7 @butterflymedia
4 years ago

  • Type changed from defect (bug) to enhancement

This ticket was mentioned in Slack in #core by boone. View the logs.


3 years ago

#9 @boonebgorges
3 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.


3 years ago

#11 @pcarvalho
3 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

@pcarvalho
3 years ago

adds required attribute and a test for it

@pcarvalho
3 years ago

fixed the inline documentation for the test

#12 @boonebgorges
3 years ago

In 37463:

Beautify, simplify, lovelify, alignify the default argument array in wp_dropdown_categories().

See #31909.

#13 @boonebgorges
3 years ago

In 37464:

Move wp_dropdown_categories() tests into their own file.

See #31909.

#14 @boonebgorges
3 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 37465:

Introduce required argument for wp_dropdown_categories().

This allows the HTML5 required attribute to be added to the select element.

Props wzislam, pcarvalho.
Fixes #31909.

Note: See TracTickets for help on using tickets.