Add priority to form validators

Created on 24 October 2023, 8 months ago

Problem/Motivation

Form API validators have no priority apart from the order they are set in a form element in the '#element_validate'. It would be nice to be able to define a validation priority so the order they are executed can be defined. When ✨ One form validation function should be able to bypass other validation steps Needs review is merge this will allow to validate first certain elements that can stop form validation. Example: a ClamAV file field should only be tested when all other validations have passed.

Steps to reproduce

Just try to set the execution order of the validation handlers on different elements in a form. There's no mechanism to do that for validators placed in different elements.

Proposed resolution

Modify the Form API validation process to collect all validation handlers, sort them according a priority and execute them using that priority.

The current format of '#element_validate' is "Array of callables or function names, which are called to validate the input. Arguments: $element, $form_state, $form". We could add another option, an array with two properties:

  • callable: the callable or function name
  • weight: numeric value to sort validator handlers as core does in many other places with other elements that need to be prioritized

Currently, FormValidator::validateForm starts validation processs validating the Form token and then traverses the form array running elements validators from bottom to top (see Form API workflow diagram β†’ ) thanks to the recursive function FormValidator::doValidateForm.

For security reasons I would say it is ok to always start validating the form with the token validation and then process the rest of the form instead of adding the token validation to the sortable array of validators. This way the token is always processed and can't be removed or deprioritized by mistake.

Hence, FormValidator::validateForm should setup an array where FormValidator::doValidateForm pushes the validator handlers found whille traversing the form array. After running FormValidator::doValidateForm and collecting all validators FormValidator::validateForm can start running validators sorting them by priority.

#element_validate in the current format (a function name or calllable) will be added to the array with a default priority.

When collecting validators we need two values: the validator callback and the $element.

Currently, the core validations are run (maxlength, required fields, options) and then the element validator. F

Not sure what to do with core validations:

  • Run core validations while traversing and collecting element's validators: this is potentially less disruptive and simpler, but the whole issue point is not completely fulfilled
  • Manage core validations as regular validators, collecting and adding them to the array of validators that will be run. This is potentially more disruptive, complex and needs core validations to have priority but completely fulfills the issue target

Anyway, validators without priority (currently all of them) will be collected and assigned the same priority, so to keep the same running order the validators should be handled as a FIFO (a priority FIFO actually).

Caveats

This is a big internal Form API change and could impact current codebase in a unforeseen ways.

Remaining tasks

Decide the way to do this and implement it.

User interface changes

None.

API changes

None really but internally the way the validators are handled will change. However, old code should work the same.

Data model changes

#element_validate now should accept callables, function names and also the same but with priority:

'#element_validate' = [
  'myFunction',
  $myCallable,
  [ 
    '#weight' => 100,
    '#handler' => 'anohterFunction'
  ],
  [ 
    '#weight' => 150,
    '#handler' => $anotherCallable
  ],  

Release notes snippet

TBD

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component
FormΒ  β†’

Last updated about 1 hour ago

Created by

πŸ‡ͺπŸ‡ΈSpain tunic Madrid

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024