You are here

public function FieldValidationRuleSetAddForm::validateForm in Field Validation 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/FieldValidationRuleSetAddForm.php, line 90

Class

FieldValidationRuleSetAddForm
Controller for FieldValidationRuleSet addition forms.

Namespace

Drupal\field_validation\Form

Code

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

  // The fieldValidationRule configuration is stored in the 'data' key in the form,
  // pass that through for validation.
  $entity_type = $form_state
    ->getValue('entity_type');
  $bundle = $form_state
    ->getValue('bundle');
  $ruleset_name = $entity_type . '_' . $bundle;
  $ruleset = \Drupal::entityTypeManager()
    ->getStorage('field_validation_rule_set')
    ->load($ruleset_name);
  if (empty($ruleset)) {
    $form_state
      ->setValue('name', $entity_type . '_' . $bundle);
    $form_state
      ->setValue('label', $entity_type . ' ' . $bundle . ' ' . 'validation');
  }
  else {
    $form_state
      ->setErrorByName('bundle', $this
      ->t('A field validation rule set already exists for this bundle'));
  }
}