You are here

public function BUEditorButtonForm::validateForm in BUEditor 8

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

src/Form/BUEditorButtonForm.php, line 154

Class

BUEditorButtonForm
Base form for BUEditor Button entities.

Namespace

Drupal\bueditor\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $bueditor_button = $this
    ->getEntity();
  $values = $form_state
    ->getValues();

  // Add ID prefix
  if (!$form_state
    ->getError($form['id'])) {
    $id = 'custom_' . $values['id'];
    $form_state
      ->setValue('id', $id);

    // Check duplicate.  Entity contains the submitted values
    if ($id != $bueditor_button
      ->getOriginalId()) {
      if ($bueditor_button
        ->load($id)) {
        $form_state
          ->setError($form['id'], $this
          ->t('The machine-readable name is already in use. It must be unique.'));
      }
    }
  }

  // Template button
  if (!empty($values['is_template'])) {
    $form_state
      ->setValue('code', '');
  }
  else {
    $form_state
      ->setValue('template', '');

    // Check class name
    if (!empty($values['cname']) && preg_match('/[^a-zA-Z0-9\\-_ ]/', $values['cname'])) {
      $form_state
        ->setErrorByName('cname', $this
        ->t('@field contains invalid characters.', [
        '@field' => $this
          ->t('Class name'),
      ]));
    }

    // Check shortcut
    if (!empty($values['shortcut']) && preg_match('/[^a-zA-Z0-9\\+]/', $values['shortcut'])) {
      $form_state
        ->setErrorByName('shortcut', $this
        ->t('@field contains invalid characters.', [
        '@field' => $this
          ->t('Shortcut'),
      ]));
    }
  }

  // Convert libraries to array.
  if (isset($values['libraries']) && is_string($values['libraries'])) {
    $form_state
      ->setValue('libraries', array_values(array_filter(array_map('trim', explode(',', $values['libraries'])))));
  }
  return parent::validateForm($form, $form_state);
}