You are here

public function SimplePopupBlocksAddForm::validateForm in Simple Popup Blocks 8

Same name and namespace in other branches
  1. 8.2 src/Form/SimplePopupBlocksAddForm.php \Drupal\simple_popup_blocks\Form\SimplePopupBlocksAddForm::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 FormInterface::validateForm

1 method overrides SimplePopupBlocksAddForm::validateForm()
SimplePopupBlocksEditForm::validateForm in src/Form/SimplePopupBlocksEditForm.php
Form validation handler.

File

src/Form/SimplePopupBlocksAddForm.php, line 256

Class

SimplePopupBlocksAddForm
Form to add a popup entry.

Namespace

Drupal\simple_popup_blocks\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getValue('type') == 0) {
    $selector = 'block_list';
    $identifier = $form_state
      ->getValue($selector);
  }
  else {
    $selector = 'custom_css';
    $identifier = $form_state
      ->getValue($selector);
  }
  if (!isset($identifier) || empty($identifier)) {
    $form_state
      ->setError($form[$selector], $this
      ->t('You should provide some css selector.'));
  }
  if ($form_state
    ->getValue('type') == 1) {

    // Get the first character using substr.
    $firstCharacter = substr($identifier, 0, 1);
    if (in_array($firstCharacter, [
      '.',
      '#',
    ])) {
      $form_state
        ->setError($form[$selector], $this
        ->t('Selector should not start with . or # in %field.', [
        '%field' => $identifier,
      ]));
    }
  }
  $check = SimplePopupBlocksStorage::loadCountByIdentifier($identifier, $this->database, $this->messenger);
  if ($check) {
    $form_state
      ->setError($form[$selector], $this
      ->t('Already popup created with this identifier %field.', [
      '%field' => $identifier,
    ]));
  }
  $trigger_method = $form_state
    ->getValue('trigger_method');
  if ($trigger_method == 1) {
    $trigger_selector = $form_state
      ->getValue('trigger_selector');

    // Get the first character using substr.
    $firstCharacter = substr($trigger_selector, 0, 1);
    if (!in_array($firstCharacter, [
      '.',
      '#',
    ])) {
      $form_state
        ->setError($form['trigger_selector'], $this
        ->t('Selector should start with . or # in %field.', [
        '%field' => $trigger_selector,
      ]));
    }
  }
}