You are here

public function CclAddForm::validateForm in Custom Contextual Links 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/CclAddForm.php, line 235
Contains \Drupal\ccl\Form\CclAddForm.

Class

CclAddForm

Namespace

Drupal\ccl\Form

Code

public function validateForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();

  // Check that that we can get the node id and transform it for saving.
  if ($values['ccl_type'] == 'node' && $values['node_options'] == 'node') {
    if (is_numeric($values['node_id'])) {
      $form_state
        ->setValueForElement($form['options_group']['node_id'], $values['node_id']);
    }
    elseif (preg_match('/\\[nid:[0-9]*\\]/', $values['node_id'], $match)) {
      $form_state
        ->setValueForElement($form['options_group']['node_id'], substr(rtrim($match[0], ']'), 5));
    }
    else {
      $form_state
        ->setErrorByName('nid', t('Enter a node id or use the autocomplete widget to select an existing node.'));
    }
  }
}