Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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');
Comment | File | Size | Author |
---|---|---|---|
#1 | drupal8.form-prepare.1.patch | 2.38 KB | sun |
Comments
Comment #1
sunHeh, simple.
Comment #2
sunoh, ok :) drupal_prepare_form() also invokes hook_form_alter() implementations at the end, so that needs to be separated into a new drupal_alter_form() or similar...
Comment #3
mgiffordShould we consider this for D9?
Comment #4
maijs CreditAttribution: maijs commentedThis issue also persists in configuration forms of Drupal 8 core modules which extend
ConfigFormBase
class. At this point even Drupal 8 core modules do not play well together as described in #2355053: ConfigFormBase::buildForm() is missing a default submit handler.