NegotiationBrowserForm::validateForm

public NegotiationBrowserForm::validateForm(array &$form, FormStateInterface $form_state)

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 152

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);
}
doc_Drupal
2016-10-29 09:29:57
Comments
Leave a Comment

Please login to continue.