You are here

public function AddToHeadProfileForm::validateForm in Add To Head 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/AddToHeadProfileForm.php, line 121

Class

AddToHeadProfileForm

Namespace

Drupal\add_to_head\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $name = $form_state
    ->getValue('name');
  $settings = \Drupal::config('add_to_head.settings')
    ->get('add_to_head_profiles');
  if (preg_match('/[^a-z0-9\\-]/', $name)) {
    $form_state
      ->setErrorByName('name', $this
      ->t('The name should only contain lower case letters, numbers and hyphens.'));
    return;
  }
  if ($name != $form_state
    ->getValue('name_orig') && isset($settings[$name])) {
    $form_state
      ->setErrorByName('name', $this
      ->t('This name has already been used. Please try another.'));
  }
}