1
  1. Created a content type and added a field "list(text)".
  2. Under field settings marked the field as required.( Please make a note of an option for default value where the value is set to None by default).
  3. Started creating content and given all the mandatory fields and left the value of the select-list field to default like 'select a default value' and try to submit the content.
  4. The form gets submitted without HTML5 validation(i.e it is not showing a popup that the field is required to the user while filling the form.) but server side validation is in place so one can observe form error after submitting the form that field list is required.

Noticed the reason behind the HTML5 validation not working is the value for the option is a string '_none'.

How to remove the default value "_none" from select-list.

3

First unset the value like below.

unset($form['field_registration_type']['widget']['#options']['_none']);

Then set your drop down value to null, like below.

$form['field_registration_type']['widget']['#options'] = ['' => 'Select'] + $form['field_registration_type']['widget']['#options'];

Now your drop down value is null and your validation will work as expected.

0

istead :

$form['field_registration_type']['widget']['#options'] = ['' => 'Select'] +$form['field_registration_type']['widget']['#options'];

i would use:

$form['field_name']['widget']['#empty_option'] = "- Select - ";
$form['field_name']['widget']["#empty_value"] = "";

i think this is more clear and you don't need to change all options, but one

happy coding, Adam

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.