public function ConditionalFieldForm::validateForm in Conditional Fields 8
Same name and namespace in other branches
- 4.x src/Form/ConditionalFieldForm.php \Drupal\conditional_fields\Form\ConditionalFieldForm::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/ ConditionalFieldForm.php, line 148
Class
- ConditionalFieldForm
- Class ConditionalFieldForm.
Namespace
Drupal\conditional_fields\FormCode
public function validateForm(array &$form, FormStateInterface $form_state) {
$table = $form_state
->getValue('table');
if (empty($table['add_new_dependency']) || !is_array($table['add_new_dependency'])) {
parent::validateForm($form, $form_state);
}
$conditional_values = $table['add_new_dependency'];
if (array_key_exists('dependee', $conditional_values) && array_key_exists('dependent', $conditional_values)) {
$dependent = $conditional_values['dependent'];
$state = isset($conditional_values['state']) ? $conditional_values['state'] : NULL;
$instances = $this->entityFieldManager
->getFieldDefinitions($form_state
->getValue('entity_type'), $form_state
->getValue('bundle'));
foreach ($dependent as $field) {
if ($conditional_values['dependee'] == $field) {
$form_state
->setErrorByName('dependee', $this
->t('You should select two different fields.'));
$form_state
->setErrorByName('dependent', $this
->t('You should select two different fields.'));
}
if (!empty($instances[$field]) && $this
->requiredFieldIsNotVisible($instances[$field], $state)) {
$field_instance = $instances[$field];
$form_state
->setErrorByName('state', $this
->t('Field %field is required and can not have state %state.', [
'%field' => $field_instance
->getLabel() . ' (' . $field_instance
->getName() . ')',
'%state' => $this->list
->conditionalFieldsStates()[$state],
]));
}
}
}
parent::validateForm($form, $form_state);
}