Skip to content

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

Description

@hiboudev

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions