You are here

public function SplashifyGroupEntityForm::validateForm in Splashify 8.2

Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level validation handler, otherwise an exception will be thrown.

Overrides ContentEntityForm::validateForm

File

src/Form/SplashifyGroupEntityForm.php, line 84

Class

SplashifyGroupEntityForm
Form controller for Splashify group entity edit forms.

Namespace

Drupal\splashify\Form

Code

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

  // If they have the opposite option checked, the "All" option should not be
  // selected.
  $values = $form_state
    ->getValues();
  if ($values['field_opposite']['value']) {
    if ($values['field_where'][0]['value'] == 'all') {
      $form_state
        ->setError($form['field_where']['widget'], $this
        ->t('The "All" option cannot be selected when checking the "Opposite" option.'));
    }
  }

  // Make sure each path is valid.
  $paths = $values['field_list_pages'][0]['value'];
  $what_paths = preg_split('/[\\n\\r]+/', $paths);
  $errors = array();
  foreach ($what_paths as $path) {

    // If this is a valid Drupal path.
    if ($is_valid = \Drupal::service('path.validator')
      ->isValid($path)) {
      continue;
    }

    // Now check if this is a url value.
    if (substr($path, 0, 7) == 'http://') {
      continue;
    }

    // This path is not an alias or the source url.
    $errors[] .= t('The path "@path" is not valid.', array(
      '@path' => $path,
    ));
  }

  // Since there could be multiple errors for this one field, we want to
  // break each error into a separate line.
  if (count($errors) > 0) {
    $form_state
      ->setError($form['field_list_pages']['widget'], implode('<br />', $errors));
  }

  // Require field_list_pages values.
  if (empty($paths) && $values['field_where'][0]['value'] == 'list') {
    $form_state
      ->setError($form['field_list_pages']['widget'], $this
      ->t("Field 'List Pages' can't be empty"));
  }
}