You are here

public static function CountryAutocompleteWidget::validateElement in Country 8

Form element validate handler for country autocomplete.

File

src/Plugin/Field/FieldWidget/CountryAutocompleteWidget.php, line 91

Class

CountryAutocompleteWidget
Plugin implementation of the 'country_autocomplete' widget.

Namespace

Drupal\country\Plugin\Field\FieldWidget

Code

public static function validateElement($element, FormStateInterface $form_state) {
  if ($country = $element['#value']) {
    $countries = $element['#selectable_countries'];
    $iso2 = array_search($country, $countries);
    if (!empty($iso2)) {
      $form_state
        ->setValueForElement($element, $iso2);
    }
    else {
      $form_state
        ->setError($element, t('An unexpected country has been entered.'));
    }
  }
}