You are here

public function ConditionalFieldEditForm::validateForm in Conditional Fields 4.x

Same name and namespace in other branches
  1. 8 src/Form/ConditionalFieldEditForm.php \Drupal\conditional_fields\Form\ConditionalFieldEditForm::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/ConditionalFieldEditForm.php, line 295

Class

ConditionalFieldEditForm
An edit form for conditional fields.

Namespace

Drupal\conditional_fields\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $condition = $form_state
    ->getValue('condition');
  $allowed_values_set = [
    ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND,
    ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
    ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR,
    ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT,
  ];
  if ($condition == 'value') {
    if (in_array($form_state
      ->getValue('values_set'), $allowed_values_set) && mb_strlen(trim($form_state
      ->getValue('values')) === 0)) {
      $form_state
        ->setErrorByName('values', $this
        ->t('Field %name is required.', [
        '%name' => $this
          ->t('Set of values'),
      ]));
    }
    elseif ($form_state
      ->getValue('values_set') == ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX && mb_strlen(trim($form_state
      ->getValue('regex'))) == 0) {
      $form_state
        ->setErrorByName('regex', $this
        ->t('Field %name is required.', [
        '%name' => $this
          ->t('Regular expression'),
      ]));
    }
  }

  // Ensure the 'reset' flag is not set for non-target values.
  if (!in_array($condition, [
    '!empty',
    'empty',
    'value',
    'checked',
    '!checked',
  ])) {
    $form_state
      ->setValue('reset', 0);
  }
  parent::validateForm($form, $form_state);
}