simpliko / wpadverts

WordPress Classifieds Plugin
https://wpadverts.com/
GNU General Public License v2.0
21 stars 11 forks source link

empty value for dropdown field should be rendered #145

Closed erikdemarco closed 2 years ago

erikdemarco commented 2 years ago

If you make field a dropdown. the placeholder text value should be rendered like this:

<select id="cars">
  <option value="">Select One</option>   <!--  empty value should be rendered to post the correct value-->
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="vw">VW</option>
  <option value="audi" selected>Audi</option>
</select>

Instead WP-Adverts will render like this if empty value exist:

<select id="cars">
  <option>Select One</option>  
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="vw">VW</option>
  <option value="audi" selected>Audi</option>
</select>

The difference between both, when we select 'Select One' the posted value for version 1 would be "" (empty), the posted value for version 2 would be "Select One". So if we place "is_required" validator for version 2 field it will not work. Because the value is not empty.

the problem line is in: https://github.com/simpliko/wpadverts/blob/43025dc7cf26ab77db21978595e29c055eee6c88/includes/class-html.php#L83

gwin commented 2 years ago

The dropdowns in the forms are rendered using the adverts_field_select() function in the functions.php file and the empty option is rendered there as in your first example

https://github.com/simpliko/wpadverts/blob/43025dc7cf26ab77db21978595e29c055eee6c88/includes/functions.php#L1413

erikdemarco commented 2 years ago

try this code, empty value will not be rendered (check it in 'view source'):

add_filter( "adverts_form_load", function(){

    if( $form['name'] != "advert" ) {
        return $form;
    }

    foreach( $form["field"] as $key => $field ) {
        if( in_array($field["name"], array( "adverts_dropdown_1" ) ) ) {
            return $form;
        }
    }

    $form["field"][] =         array(
        "name" => "adverts_dropdown_1",
        "type" => "adverts_field_select",
        "order" => 12,
        "label" => __( "Dropdown 1", "wpadverts" ),
        "options" => array(
            0 => array(
                'value' => '',
                'text' => 'Choose one',
            ),
            1 => array(
                'value' => 'Option 1',
                'text' => 'Option 1',
            ),
            2 => array(
                'value' => 'Option 2',
                'text' => 'Option 2',
            ),
        ),
    );

    return $form;

} );

it will be just printed like: <option selected="selected" >Choose one</option>

gwin commented 2 years ago

The empty value cannot be in the "options" array, your field should look like this

    $form["field"][] = array(
        "name" => "adverts_dropdown_1",
        "type" => "adverts_field_select",
        "order" => 12,
        "label" => __( "Dropdown 1", "wpadverts" ),
        "empty_option" => true,
        "empty_option_text" => 'Choose one',
        "options" => array(
            0 => array(
                'value' => 'Option 1',
                'text' => 'Option 1',
            ),
            1 => array(
                'value' => 'Option 2',
                'text' => 'Option 2',
            ),
        ),
    );
erikdemarco commented 2 years ago

@gwin Ahhh my bad. Thank you I missed that.

There is another bug. See this line: https://github.com/simpliko/wpadverts/blob/43025dc7cf26ab77db21978595e29c055eee6c88/includes/functions.php#L1413

it should end with "option" tag instead of "options" tag. Like: $html .= '<option value="">'.esc_html($field["empty_option_text"]).'</option>';

gwin commented 2 years ago

I will correct it in the next release.