views:

51

answers:

2

I have created different node forms for different content types, by using this function in my template:

function mytheme_theme($existing, $type, $theme, $path) {
  return array(
    'type1_form' => array(
        'arguments' => array('form' => NULL),
        'template' => 'type1_form'
    ),
    'type2_form' => array(
        'arguments' => array('form' => NULL),
        'template' => 'type2_form'
    ),
  );
}

Now I'd like to make a validate function for one of the forms. I tried using this function in template.php:

function mytheme_form_alter(&$form, $form_state, $form_id) {
   if($form_id == 'type1_form') {
     $form['#validate'][] = 'my_sample_validate_func';
   }
}

function my_sample_validate_func($form, &$form_state) {
   dsm($form_state);
}

But apparently the hook_form_alter isn't avaliable to the theme layer. Do I have to make a new module to accomplish this?

A: 

hook_form_alter must go in a modul, not a template.php file. For most of my Drupal projects I end up creating a glue module just to hold stuff like form_alter functions.

This Lullabot article shows how to alter forms with hook_form_alter and in the theme layer, but in my opinion it is easier to do it in a separate module.

Jergason
Well then, the easiest solution is often the best. I'll make a glue module for it, hopefully I'll get other uses for it too.
Toxid
Oh you definitely will. Having a glue module opens up more customization than you can imagine with Drupal!
Chris Ridenour
A: 

Yes, you can. But simpler to create type1_form.tpl.php, type2_form.tpl.php, investigate $form via print_r or devel module.
Also you can use preprocessor of these themers.

Nikit