Working on a new field type I've found that for some reason hook_validate is being called twice. It may be deeper than content.module, but I haven't notice validate being called twice in other modules. Maybe a form/#validate issue?

CommentFileSizeAuthor
#2 validate_one.patch710 byteschx
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

decafdennis’s picture

Title: hook_validate called twice. » hook_validate called twice
Project: Content Construction Kit (CCK) » Drupal core
Version: 6.x-1.x-dev » x.y.z
Component: General » node system

Yes, the validate functions are called by drupal_get_form et all, like they should be. The second time, they're called by node_form_add_preview.

I don't know whether this is a big problem for anyone, I just wanted to mention it and see how it is caused.

chx’s picture

Assigned: Unassigned » chx
Status: Active » Needs review
FileSize
710 bytes

The preview stuff happens before the drupal_validate_form call and is needed. The drupal_validate_form call is just not smart enough to know that it has been called on the same form but that can be fixed...

decafdennis’s picture

Good solution, but, wat does the #validated flag in _form_validate (http://drupaldocs.org/api/head/function/_form_validate) do exactly?

chx’s picture

that's form API level checker but if you look at the code flow it's a copy of the form that's changed.

hunmonk’s picture

+1, code looks good. this should work ok w/ multipage forms, as the current implementation only needs to be validated once.

decafdennis’s picture

The form gets recursed by _form_validate, passing a copy of subelement to that function. What then is the use of #validated? In what situation would !$elements['#validated'] at the top of _form_validate ever result in false?

hunmonk’s picture

tested the patch using the multipart example module. all validate functionality seems to work fine.

hunmonk’s picture

naquah: #validated can be marked for any form element prior to the validation code running. probably not a common use case, but still possible...

killes@www.drop.org’s picture

Status: Needs review » Fixed

applied

chx’s picture

naquah, the whole form API is loaded with safety belts. #processed, #after_build_done, #theme_used, #printed just a few comes to my mind. If you are recursing AND such belts add little code, it's better for them to be there. I can rwite you a form array which would recurse on itself using some reference magic. But are you sure you want to have nightmares of that :) ?

Robert, ask nedjo, currently he knows best. Or read the code, that's what I'd do if I'd want to answer :)

decafdennis’s picture

chx, thanks for clearifying it... and I have enough nightmares from other things already. Thanks again, this is a good way to learn stuff.

Anonymous’s picture

Status: Fixed » Closed (fixed)