public function EditForm::validateForm in Access Filter 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/ EditForm.php, line 189
Class
- EditForm
- Provides edit filter form.
Namespace
Drupal\access_filter\FormCode
public function validateForm(array &$form, FormStateInterface $form_state) {
$parser = new Parser();
// Validate configuration for conditions.
$conditions = [];
try {
$conditions = $parser
->parse($form_state
->getValue('conditions'));
} catch (\Exception $ex) {
$form_state
->setErrorByName('conditions', $this
->t('Invalid YAML format for conditions. @exception', [
'@exception' => $ex
->getMessage(),
]));
}
$condition_plugins = $this->conditionPluginManager
->getDefinitions();
$condition_errors = [];
foreach ($conditions as $i => $condition) {
$line_errors = [];
if (isset($condition['type']) && strlen($condition['type'])) {
$plugin_id = $condition['type'];
if (isset($condition_plugins[$plugin_id])) {
$instance = $this->conditionPluginManager
->createInstance($plugin_id, $condition);
$line_errors += array_values($instance
->validateConfiguration($condition));
}
else {
$line_errors[] = $this
->t('Type @type is invalid.', [
'@type' => $plugin_id,
]);
}
}
else {
$line_errors[] = $this
->t("'@property' is required.", [
'@property' => 'type',
]);
}
if (!empty($line_errors)) {
$condition_errors[$i] = $line_errors;
}
}
if (!empty($condition_errors)) {
$form_state
->setErrorByName('conditions', '');
$this
->messenger()
->addError($this
->t('There are configuration errors in conditions.'), TRUE);
foreach ($condition_errors as $i => $line_errors) {
$this
->messenger()
->addError($this
->t('Line @line:', [
'@line' => $i + 1,
]), TRUE);
foreach ($line_errors as $error) {
$this
->messenger()
->addError($this
->t('- @error', [
'@error' => $error,
]), TRUE);
}
}
}
// Validate configuration for rules.
$rules = [];
try {
$rules = $parser
->parse($form_state
->getValue('rules'));
} catch (\Exception $ex) {
$form_state
->setErrorByName('rules', $this
->t('Invalid YAML format for rules. @exception', [
'@exception' => $ex
->getMessage(),
]));
}
$rule_plugins = $this->rulePluginManager
->getDefinitions();
$rule_errors = [];
foreach ($rules as $i => $rule) {
$line_errors = [];
if (isset($rule['type']) && strlen($rule['type'])) {
$plugin_id = $rule['type'];
if (isset($rule_plugins[$plugin_id])) {
$instance = $this->rulePluginManager
->createInstance($plugin_id, $rule);
if (!isset($rule['action']) || !strlen($rule['action'])) {
$line_errors[] = $this
->t("'@property' is required.", [
'@property' => 'action',
]);
}
elseif (!in_array($rule['action'], [
'deny',
'allow',
])) {
$line_errors[] = $this
->t("'action' should be 'deny' or 'allow'.");
}
$line_errors += array_values($instance
->validateConfiguration($rule));
}
else {
$line_errors[] = $this
->t('Type @type is invalid.', [
'@type' => $plugin_id,
]);
}
}
else {
$line_errors[] = $this
->t("'@property' is required.", [
'@property' => 'type',
]);
}
if (!empty($line_errors)) {
$rule_errors[$i] = $line_errors;
}
}
if (!empty($rule_errors)) {
$form_state
->setErrorByName('rules', '');
$this
->messenger()
->addError($this
->t('There are configuration errors in rules.'), TRUE);
foreach ($rule_errors as $i => $line_errors) {
$this
->messenger()
->addError($this
->t('Line @line:', [
'@line' => $i + 1,
]), TRUE);
foreach ($line_errors as $error) {
$this
->messenger()
->addError($this
->t('- @error', [
'@error' => $error,
]), TRUE);
}
}
}
}