You are here

public function ConfigFormBuilder::validateDisableFieldConfigForm in Disable Field 8.2

Validation rules for the disable field config form.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

File

src/ConfigFormBuilder.php, line 168

Class

ConfigFormBuilder
Class ConfigFormAlter.

Namespace

Drupal\disable_field

Code

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

  // Check if the add roles field contains values when required.
  $add_roles = $form_state
    ->getValue([
    'disable_field',
    'add',
    'roles',
  ]);
  $add_option = $form_state
    ->getValue([
    'disable_field',
    'add',
    'disable',
  ]);
  if (empty($add_roles) && in_array($add_option, [
    'roles',
    'roles_enable',
  ])) {
    $form_state
      ->setErrorByName('disable_field][add][roles', $this
      ->t('Please, choose at least one role.'));
  }

  // Check if the edit roles field contains values when required.
  $edit_roles = $form_state
    ->getValue([
    'disable_field',
    'edit',
    'roles',
  ]);
  $edit_option = $form_state
    ->getValue([
    'disable_field',
    'edit',
    'disable',
  ]);
  if (empty($edit_roles) && in_array($edit_option, [
    'roles',
    'roles_enable',
  ])) {
    $form_state
      ->setErrorByName('disable_field][edit][roles', $this
      ->t('Please, choose at least one role.'));
  }
}