Skip to content

InputfieldSelect: blank option should not be added when field is not required and a default option is selected #2068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hiboudev opened this issue Apr 30, 2025 · 0 comments

Comments

@hiboudev
Copy link

hiboudev commented Apr 30, 2025

Actually InputfieldSelect wrongly adds a blank option when field is not required and a default value is selected, I don't need and want it but can't disable it.
But when the field is required, it doesn't add the empty option when W3C says it should be, regardless of whether an option is selected (see below).

Actual code in InputfieldSelect:

if($allowBlank && !$hasBlankOption && !$this->attr('multiple')) { 
  if($this->getSetting('required') && $this->attr('value')) {
    // if required and a value is already selected, do not add a blank option
  } else {
    $out .= "<option value=''>&nbsp;</option>";
  }
}

Proposed fix:

if($allowBlank && !$hasBlankOption && !$this->attr('multiple')) { 
  if($this->getSetting('required') || !$this->attr('value')) {
    $out .= "<option value=''>&nbsp;</option>";
  } else {
  }
}

It always add empty option if no default option is selected, or if select is required.

W3C validation with a selected option and a required field:

<select class="required uk-select" id="year" name="year" required="required">
  <option  value='2024'>2024</option>
  <option selected='selected'  value='2025'>2025</option>
</select>

W3C validation:

Error: The first child option element of a select element with a required attribute, and without a multiple attribute, and without a size attribute whose value is greater than 1, must have either an empty value attribute, or must have no text content. Consider either adding a placeholder option label, or adding a size attribute with a value equal to the number of option elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant