Okay - strange question. Assume module A creates a form, and has a form_validate function in it. I need to add a little extra validation to that specific form, but can't touch module A (long story).

Can I do it with a new module (module B)?

I'm trying this just to test:

function moduleA_form_validate($form, &$form_state) 
{
   drupal_set_message('howdy');
}

Not getting any results.

Thanks

Comments

kenuck’s picture

Yes there is a trick you can use to do it.

First in your Module B, use hook_form_alter to inject (or overwrite) your own validation function into the "#validate" attribute of said form. (example => http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....)

Then your validation function will also be called.

cheers

vertikal.dk’s picture

wheelercreek,

If you want to preserve the existing validation and submit processing, you can poke your own function in front the queue:


function moduleB_form_alter(&$form, $form_state, $form_id) {
        switch ($form_id) {
                case 'the_form_i_want' :
                        array_unshift($form['#validate'], 'moduleB_my_special_validate');
                        // To put it after use $form['#validate'][]='moduleB_my_special_validate';
        }
}

function moduleB_my_special_validate($form, &$form_state) {
        // Do whatever
}

This should ensure that your function is called before (or after) the remaining list of validation handlers - whatever they might be.

Martin