- Issue created by @tunic
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.
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.
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:
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:
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).
This is a big internal Form API change and could impact current codebase in a unforeseen ways.
Decide the way to do this and implement it.
None.
None really but internally the way the validators are handled will change. However, old code should work the same.
#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
],
TBD
Active
11.0 π₯
Last updated