I would like to interleave the processing before the Core validation of the FormAPI.

As for the processing flow of FormAPI, buildForm() -> [submit] -> Core Validation-> validateForm() -> submitForm() The flow of FormAPI processing is as follows

I would like to process the submitted value before the validation by Core. The reason is that in the 'datelist' type of FormAPI, passing a string type as the value value will result in a 500 error. The following Issue has been submitted but there is no indication in Drupal 10 that it will be supported. https://www.drupal.org/project/drupal/issues/3318851

So I would like to do the following in custom_module.module etc.

$value = $form_state->getValue('birthday');
$form_state->setValue('birthday', intval($value));

What should I do?

  • That issue isn’t moving because it is set to the wrong component (documentation) and because contributors have not done what the maintainer asked.
    – mona lisa
    Commented Apr 13 at 11:04

1 Answer 1


You can set #element_validate on the form element, and this validation will happen before the default/full form validation.


$form['birthday'] = [
  '#element_validate' => ['MODULE_FORMID_element_validate'],
  // Or if you are defining the form in the same class as the
  // element is defined:
  '#element_validate' => ['::elementValidate'],

In the validation handler, you can set the value on the form element in the manner you showed:

function MODULE_FORMID_element_validate(array &$form, FormStateInterface $form_state) {
  $value = $form_state->getValue('birthday');
  $form_state->setValue('birthday', intval($value));
  • Thank you for your response. I tried that but it did not work. The custom validation does not go in before Core. The order seems to be as follows. buildForm() -> [submit] -> Core Validation-> "MODULE_FORMID_element_validate() / elementValidate()" -> validateForm() -> submitForm()
    – Skyyade
    Commented Apr 15 at 1:10
  • I think I may have found the cause. Perhaps the 'datelist' type has a value generated by DrupalDateTime::createFromArray. It seems that this is happening because checkArray() is called in it. Do you know how to work around this?
    – Skyyade
    Commented Apr 15 at 1:28
  • I'm not sure how you are testing, but #element_validate handlers are called before form validation handlers.
    – Jaypan
    Commented Apr 15 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.