You are here

public static function YamlFormOtherBase::validateYamlFormOther in YAML Form 8

Validates an other element.

File

src/Element/YamlFormOtherBase.php, line 170

Class

YamlFormOtherBase
Base class for form other element.

Namespace

Drupal\yamlform\Element

Code

public static function validateYamlFormOther(&$element, FormStateInterface $form_state, &$complete_form) {

  // Remove 'yamlform_' prefix from type.
  $type = str_replace('yamlform_', '', static::$type);
  $element_value = $element[$type]['#value'];
  $other_value = $element['other']['#value'];
  if (static::isMultiple($element)) {
    $value = $element_value;
    if (isset($element_value[static::OTHER_OPTION])) {
      unset($value[static::OTHER_OPTION]);
      if ($other_value === '') {
        static::setOtherError($element, $form_state);
        return;
      }
      else {
        $value[$other_value] = $other_value;
      }
    }
    $is_empty = empty($value) ? TRUE : FALSE;
  }
  else {
    $value = $element_value;
    if ($element_value == static::OTHER_OPTION) {
      if ($other_value === '') {
        static::setOtherError($element, $form_state);
        return;
      }
      else {
        $value = $other_value;
      }
    }
    $is_empty = $value === '' || $value === NULL ? TRUE : FALSE;
  }
  $has_access = !isset($element['#access']) || $element['#access'] === TRUE;
  if ($element['#required'] && $is_empty && $has_access) {
    static::setElementError($element, $form_state);
  }
  $form_state
    ->setValueForElement($element[$type], NULL);
  $form_state
    ->setValueForElement($element['other'], NULL);
  $form_state
    ->setValueForElement($element, $value);
}