form API has hook_form_validate_alter and hook_form_execute_alter, which allow mods to have a say in the validation/execution of any form. however, the alter functions are kind of limited if they do not provide the ability to override the validate/execute callbacks of the form itself. i've already run into this problem in a custom login module that i'm upgrading. after a discussion with adrian, this patch introduces attributes #validate_override and #execute_override, which makes this functionality possible. normal use case would be to set these to TRUE in a hook_form_alter if overriding was necessary.

Comments

chx’s picture

Status: Needs review » Reviewed & tested by the community

More power to the form API in a two line patch, that's always good

adrian’s picture

+1 =)

hunmonk’s picture

StatusFileSize
new1.58 KB

found one bug in further testing-- have to test in $form array, not $form_values array :)

just finished using this functionality in a custom login mod, and it works great!

adrian’s picture

Upon further thought ,

why not call the properties '#validate' and '#execute' , which defaults to form_id_validate and form_id_execute, but can be overridden.

This is the same way the #theme property works.

dries’s picture

It looks to me as if this is getting more complex every minute, at the same time, it looks as if this is somewhat necessary to make extensive form modifications work elegantly. (The problem is that we have per-form validation and not per-element validation.)

I'm wrapping my head around this some more ... I don't understand what Adrian is suggesting. Adrian, are you talking about per-form properties or what?

hunmonk’s picture

Title: form validate/execute functions can't be overridden » new workflow for form validation/execution
StatusFileSize
new8.45 KB

adrian and i spent the morning working out this new workflow, which i believe is far superior to the old. changes this patch makes:

  1. hook_form_validate_alter and hook_form_execute_alter have been eliminated. there usage was limiting and a bit confusing. now, only hook_form_alter remains
  2. replacing those hooks are the #validate and #execute attributes. these can be used either when building the form in a module, or via hook_form_alter. these attributes are built in array form, to allow for flexibility in how they are called. for example, a module could simply add an element to the #validate array via hook_form_alter if it wished to perform additional validation, or it could redeclare the entire #validate array if it wanted to override any existing validation
  3. attributes #validation_arguments and #execution_arguments have been added to allow args to be used w/ custom validation and execution functions. this are also declared in array format
  4. renamed #valid attribute to #validation. this doesn't cause any conflicts, and makes the naming convention more consistent
  5. renamed the existing #execute attribute associated w/ submit buttons to #submit
hunmonk’s picture

btw, i tested this new functionality w/ logintoboggan, which makes heavy use of it. works great :)

chx’s picture

Status: Reviewed & tested by the community » Needs work
drewish’s picture

i haven't tested the patch yet but i definitely prefer this to the _alter hooks.

chx’s picture

Status: Needs work » Needs review
StatusFileSize
new8.23 KB

I did little work here, the patch is still hunmonk's , I just fixed a few typos and renamed #submit / $submit to form_submitted. This is a prelude to a sweeping change...

chx’s picture

Status: Needs review » Reviewed & tested by the community
dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to HEAD. Please update the documentation accordingly. Thanks.

chx’s picture

Assigned: hunmonk » chx
Priority: Normal » Critical
Status: Fixed » Reviewed & tested by the community
StatusFileSize
new889 bytes

I deleted too much and this broke a "few" things like login. While it's possible to merge the per form and the per element , for now, it does not worth the bothering.

junyor’s picture

That fixed site login for me, thanks!

drumm’s picture

This definately helps. For the login form the username on the navigation block changes properly, but the login block remains until the page is reloaded.

drumm’s picture

As far as I can tell, HEAD now only executes the form code when drupal_get_form() is called. This can be solved (after applying the latest patch here) for the user login block by puttng the form generation code above the test to see if the user is logged in. Unfortunately this is still too late in the page generation for the menu system to get the right permissions.

The best solution I can think of is putting the login form generation in user_init().

junyor’s picture

Isn't that the same problem as seen with file uploads in http://drupal.org/node/37605?

hunmonk’s picture

StatusFileSize
new1.15 KB

chx: don't you think it would be a good idea if the button element types also had #form_submitted attributes?? :P

currently all drupal forms are broken until this patch hits core.

webchick’s picture

StatusFileSize
new2.07 KB

Here's a single patch which combines both required patches.

webchick’s picture

Oh and +1.. I can register/login now. :)

drumm’s picture

Works perfectly with this patch
+1

chx’s picture

Sorry -- I really do not know what happened but system module dd not get updated when the original patch went in :(

chx’s picture

StatusFileSize
new2.62 KB

Filter also needs a little love. Only form.inc got updated.

chx’s picture

StatusFileSize
new2.64 KB

Rerolled so it can be applied from drupal root.

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to HEAD. Thanks.

chx’s picture

Status: Fixed » Reviewed & tested by the community

I just can't get this right. http://drupal.org/node/38349#comment-55931 is still needed sorry all.

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to HEAD. Thanks.

Anonymous’s picture

Status: Fixed » Closed (fixed)