Drupal 10: Getting A Custom Form Validation Handler To Run First

In order to get a form validation handler to run first we need to put it at the beginning of the validation handler array. This is done using array_unshift.

Here is an example of setting a form validation to the login form using a hook_form_FORM_ID_alter() hook.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  array_unshift($form['#validate'], 'mymodule_login_form_validate');
}

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
3 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.