Make WordPress Core

Changeset 37465


Ignore:
Timestamp:
05/19/2016 02:38:33 AM (8 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/category-template.php

    r37463 r37465  
    315315 * @since 2.1.0
    316316 * @since 4.2.0 Introduced the `value_field` argument.
     317 * @since 4.6.0 Introduced the `required` argument.
    317318 *
    318319 * @param string|array $args {
     
    352353 *     @type bool         $hide_if_empty     True to skip generating markup if no categories are found.
    353354 *                                           Default false (create select element even if no categories are found).
     355 *     @type bool         $required          Whether the <select> element should have the HTML5 'required' attribute.
     356 *                                           Default false.
    354357 * }
    355358 * @return string HTML content only if 'echo' argument is 0.
     
    377380        'option_none_value' => -1,
    378381        'value_field'       => 'term_id',
     382        'required'          => false,
    379383    );
    380384
     
    415419    $class = esc_attr( $r['class'] );
    416420    $id = $r['id'] ? esc_attr( $r['id'] ) : $name;
     421    $required = $r['required'] ? 'required' : '';
    417422
    418423    if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
    419         $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
     424        $output = "<select $required name='$name' id='$id' class='$class' $tab_index_attribute>\n";
    420425    } else {
    421426        $output = '';
  • trunk/tests/phpunit/tests/category/wpDropdownCategories.php

    r37464 r37465  
    154154        }
    155155    }
     156
     157    /**
     158     * @ticket 31909
     159     */
     160    public function test_required_true_should_add_required_attribute() {
     161        // Create a test category.
     162        $cat_id = self::factory()->category->create( array(
     163            'name' => 'Test Category',
     164            'slug' => 'test_category',
     165        ) );
     166
     167        $args = array(
     168            'show_option_none'  => __( 'Select one', 'text-domain' ),
     169            'option_none_value' => "",
     170            'required'          => true,
     171            'hide_empty'        => 0,
     172            'echo'              => 0,
     173        );
     174        $dropdown_categories = wp_dropdown_categories( $args );
     175
     176        // Test to see if it contains the "required" attribute.
     177        $this->assertRegExp( '/<select[^>]+required/', $dropdown_categories );
     178    }
     179
     180    /**
     181     * @ticket 31909
     182     */
     183    public function test_required_false_should_omit_required_attribute() {
     184        // Create a test category.
     185        $cat_id = self::factory()->category->create( array(
     186            'name' => 'Test Category',
     187            'slug' => 'test_category',
     188        ) );
     189
     190        $args = array(
     191            'show_option_none'  => __( 'Select one', 'text-domain' ),
     192            'option_none_value' => "",
     193            'required'          => false,
     194            'hide_empty'        => 0,
     195            'echo'              => 0,
     196        );
     197        $dropdown_categories = wp_dropdown_categories( $args );
     198
     199        // Test to see if it contains the "required" attribute.
     200        $this->assertNotRegExp( '/<select[^>]+required/', $dropdown_categories );
     201    }
     202
     203    /**
     204     * @ticket 31909
     205     */
     206    public function test_required_should_default_to_false() {
     207        // Create a test category.
     208        $cat_id = self::factory()->category->create( array(
     209            'name' => 'Test Category',
     210            'slug' => 'test_category',
     211        ) );
     212
     213        $args = array(
     214            'show_option_none'  => __( 'Select one', 'text-domain' ),
     215            'option_none_value' => "",
     216            'hide_empty'        => 0,
     217            'echo'              => 0,
     218        );
     219        $dropdown_categories = wp_dropdown_categories( $args );
     220
     221        // Test to see if it contains the "required" attribute.
     222        $this->assertNotRegExp( '/<select[^>]+required/', $dropdown_categories );
     223    }
    156224}
Note: See TracChangeset for help on using the changeset viewer.