You are here

public static function UnlimitedNumber::validateUnlimitedNumber in Unlimited Number Field 8

Same name and namespace in other branches
  1. 2.x src/Element/UnlimitedNumber.php \Drupal\unlimited_number\Element\UnlimitedNumber::validateUnlimitedNumber()

Form element validation handler for unlimited_number elements.

See also

getInfo().

File

src/Element/UnlimitedNumber.php, line 128

Class

UnlimitedNumber
Provides an unlimited or number radios element

Namespace

Drupal\unlimited_number\Element

Code

public static function validateUnlimitedNumber(array &$element, FormStateInterface $form_state, array &$complete_form) {
  $value = NULL;
  if ($element['unlimited_number']['#value'] == 'unlimited') {
    $value = static::UNLIMITED;
  }
  else {
    if ($element['unlimited_number']['#value'] == 'limited') {

      // If limited radio is checked, number field is required.
      $limited = $element['unlimited_number']['limited']['number']['#value'];
      if (is_numeric($limited)) {
        $value = $limited;
      }
      else {
        $form_state
          ->setError($element['unlimited_number']['limited']['number'], t('A number must be entered. Otherwise choose @unlimited.', [
          '@unlimited' => $element['unlimited_number']['unlimited']['radio']['#title'],
        ]));
      }
    }
  }
  $form_state
    ->setValueForElement($element, $value);
}