Calling form validate functions in include files from other modules

Update: See comments below, and completely ignore this post. Nothing to see here...

module_load_include() is a great way to add code from other module's include files, but it doesn't always work as you'd expect. Recently, I was building a form in one module that pulled up a validation function from another module when a particular submit button was pressed:

<?php
  module_load_include
('inc', 'another_module', 'includes/another_module.forms');
 
$form['actions']['submit'] = array(
   
'#type' => 'submit',
   
'#value' => t('Awesome Submit Button'),
   
'#validate' => array('another_module_form_validate_function'),
  );
?>

I thought just adding in the module_load_include() would work, but alas, there was more to it than that. Instead of going about it this way, I had to call a local form validation function, and in that function, I could load the include from another module and call it's validation function:

<?php
   
'#validate' => array('same_module_form_validate'),
...
function
same_module_form_validate($form, &$form_state) {
 
// Pass off validation to other module.
 
module_load_include('inc', 'another_module', 'includes/another_module.forms');
 
another_module_form_validate_function($form, $form_state);
}
?>

This helps me uphold DRY principles, and reuse specific form validation functions from other module's include files. (Typically, though, if I were going to be validating a particular element, or a bunch of different forms, using the same validation function, I would include that validation in the .module file itself so I could call it anywhere without a module_load_include()... but in this case, I didn't want that particular validation function to have to be in memory on every Drupal page request :).

Comments

Umm... Yeah. Didn't even see that. I was looking for any way of doing everything inside the form itself, but for some reason didn't find that function. I'll update the post with this information.

You don't say why the first solution didn't work, but are you aware of form_load_include()? It might be what you were looking for.

+1 for form_load_include (d7). I've used it to include custom vaidators and submit functions and it works great.