You are here

public function ItemForm::validateForm in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Form/ItemForm.php \Drupal\business_rules\Form\ItemForm::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/ItemForm.php, line 480

Class

ItemForm
Base class for Business rules item.

Namespace

Drupal\business_rules\Form

Code

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

  // Validate machine name. We don't want actions and conditions with the same
  // machine name because Business Rules use this items together and it's id
  // as the array key.
  $id = $form_state
    ->getValue('id');
  if ($id && $this->entity
    ->isNew()) {
    $action = Action::load($id);
    $condition = Condition::load($id);
    if (!empty($action) || !empty($condition)) {
      $form_state
        ->setErrorByName('id', $this
        ->t('The machine-readable name is already in use. It must be unique.'));
    }
  }

  // Validate the form using the plugin validateForm() method.
  $type = $form_state
    ->getValue('type');
  $definition = $this
    ->getItemManager()
    ->getDefinition($type);
  $reflection = new \ReflectionClass($definition['class']);
  $custom_item = $reflection
    ->newInstance($definition, $definition['id'], $definition);
  $custom_item
    ->validateForm($form, $form_state);
}