0

On Drupal 7, I would like to execute some code once a form has been validated.

I have seen many usage of hook_form_FORM_ID_alter : implementing this hook and adding a custom callback to $form['#validate'] I could perform action on validation.

But I need to be absolutely sure the form has been validated to perform my actions.

Is there some other hook to do so ? Or something I could do in hook_form_alter to ensure that ?

1 Answer 1

0

You should add a submit callback instead of a validation callback. Submit callbacks are called after the form has been validated.

function yourmodule_form_FORM_ID_alter(&$form, &$form_state) {
  $form['#submit'][] = 'yourmodule_custom_submit_callback';
}

function yourmodule_custom_submit_callback($form, &$form_state) {
  // Execute here your code
}
2
  • Oh! I thought sumbit callbacks were called when the user submit the form. Are you sure about that ? On api.drupal.org/api/drupal/… it says "A list of custom submit functions that will be called when the element is submitted. This is usually to add additional submit functions to a form, or to use an alternate function rather than the default form submission function which is the form ID with _submit appended to it."
    – Eria
    Commented Nov 10, 2017 at 9:43
  • Submit handlers are called after the validation handlers. Otherwise it would have no meaning to validate the form after it is already submitted.
    – Elin Y.
    Commented Nov 10, 2017 at 14:27

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.