You are here

public function NegotiationBrowserForm::validateForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/language/src/Form/NegotiationBrowserForm.php \Drupal\language\Form\NegotiationBrowserForm::validateForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

core/modules/language/src/Form/NegotiationBrowserForm.php, line 157
Contains \Drupal\language\Form\NegotiationBrowserForm.

Class

NegotiationBrowserForm
Configure the browser language negotiation method for this site.

Namespace

Drupal\language\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  // Array to check if all browser language codes are unique.
  $unique_values = array();

  // Check all mappings.
  if ($form_state
    ->hasValue('mappings')) {
    $mappings = $form_state
      ->getValue('mappings');
    foreach ($mappings as $key => $data) {

      // Make sure browser_langcode is unique.
      if (array_key_exists($data['browser_langcode'], $unique_values)) {
        $form_state
          ->setErrorByName('mappings][new_mapping][browser_langcode', $this
          ->t('Browser language codes must be unique.'));
      }
      elseif (preg_match('/[^a-z\\-]/', $data['browser_langcode'])) {
        $form_state
          ->setErrorByName('mappings][new_mapping][browser_langcode', $this
          ->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
      }
      $unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
    }
  }

  // Check new mapping.
  $data = $form_state
    ->getValue('new_mapping');
  if (!empty($data['browser_langcode'])) {

    // Make sure browser_langcode is unique.
    if (array_key_exists($data['browser_langcode'], $unique_values)) {
      $form_state
        ->setErrorByName('mappings][' . $key . '][browser_langcode', $this
        ->t('Browser language codes must be unique.'));
    }
    elseif (preg_match('/[^a-z\\-]/', $data['browser_langcode'])) {
      $form_state
        ->setErrorByName('mappings][' . $key . '][browser_langcode', $this
        ->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
    }
    $unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
  }
  $form_state
    ->set('mappings', $unique_values);
}