Run drupal_prepare_form() before the form constructor instead of after

Created on 24 February 2012, over 13 years ago
Updated 14 April 2025, 2 months ago

Problem

  • Adding to #validate, #submit, or #theme in a form constructor destroys the default handlers.

Goal

  • Retain the default form handlers, unless specifically overridden.

Details

  • We introduced the obligatory $form parameter for form constructor functions very late in D7.
  • Given the $form argument, the default #validate, #submit, and #theme properties could already be pre-populated in there, instead of attempting to apply them after the constructor ran.
  • drupal_prepare_form() would effectively run before the form constructor - instead of after.
  • Thus, $form['#validate'][] = 'foo_form_validate'; does what you think it does.
  • Only $form['#validate'] = array('foo_form_validate'); effectively overrides (and again, does what you think it does).
  • All of this is limited to the form constructor function (which exists only once). No impact on hook_form_alter() and the like.

API change

D7:

  $form['#validate'][] = 'foo_form_validate';

... (un)intentionally disables the default form validation handler.

D8:

  // Add a form validation handler.
  $form['#validate'][] = 'foo_form_validate';
  // Intentionally replace the default form validation handler.
  $form['#validate'] = array('foo_form_validate');
📌 Task
Status

Postponed: needs info

Version

11.0 🔥

Component

forms system

Created by

🇩🇪Germany sun Karlsruhe

Live updates comments and jobs are added and updated live.
  • Security

    It is used for security vulnerabilities which do not need a security advisory. For example, security issues in projects which do not have security advisory coverage, or forward-porting a change already disclosed in a security advisory. See Drupal’s security advisory policy for details. Be careful publicly disclosing security vulnerabilities! Use the “Report a security vulnerability” link in the project page’s sidebar. See how to report a security issue for details.

  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States smustgrave

    Thank you for creating this issue to improve Drupal.

    We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024