Using a form alter hook, we can add additional validators to a form. The proper way to do this is to load the current validators and add the new one to the array and reset the validators in the form state:
$validators = $form_state->getValidateHandlers(); $validators[] = 'mymodule_form_validate'; $form_state->setValidateHandlers($validators);
First, we will receive all of the currently set validators from the form state as the $validators variable. We then append a new callback to the end of the array. Once the $validators variable has been modified, we will override the form state's validator array by executing the setValidateHandlers method.
You can also use PHP array manipulation functions to add your validators in different execution orders. For example, array_unshift will place your validator at the beginning of the array so that it can run first.