You are here

public function CustomLanguageForm::validateForm in Custom Language field 8

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

src/Form/CustomLanguageForm.php, line 107

Class

CustomLanguageForm
Form controller for the CustomLanguage entity edit forms.

Namespace

Drupal\languagefield\Form

Code

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

  // Ensure sane field values for langcode and names.
  if (!isset($form['langcode_view']) && !preg_match('@^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$@', $form_state
    ->getValue('langcode'))) {
    $form_state
      ->setErrorByName('langcode', $this
      ->t('%field must be a valid language tag as <a href=":url">defined by the W3C</a>.', [
      '%field' => $form['langcode']['#title'],
      ':url' => 'http://www.w3.org/International/articles/language-tags/',
    ]));
  }
  foreach ([
    'label',
    'native_name',
  ] as $field) {
    if ($form_state
      ->getValue($field) != Html::escape($form_state
      ->getValue($field))) {
      $form_state
        ->setErrorByName($field, $this
        ->t('%field cannot contain any markup.', [
        '%field' => $form[$field]['#title'],
      ]));
    }
  }
}