Closed erikdemarco closed 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
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>
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',
),
),
);
@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>';
I will correct it in the next release.
If you make field a dropdown. the placeholder text value should be rendered like this:
Instead WP-Adverts will render like this if empty value exist:
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