Hi all,
I am using hook_form_alter in D7. After altering the form i want call a custom written validation and submit functions. How can i call those functions after using hook_form_alter.

Thanks
drupsforyou

Comments

You add them to the #validate and #submit arrays:

<?php
function something_form_alter(&$form, &$form_state, $form_id)
{
  if(
$form_id == 'something')
  {
   
$form['#validate'][] = 'my_custom_validate_function';
   
$form['#submit'][] = 'my_custom_submit_function';
  }
}
function
my_custom_validate_function($form, &$form_state)
{
 
// validate here
}
function
my _custom_submit_function($form, &$form_state)
{
 
// submit here
}
?>

A good idea is to look at the contents of the $form array in your [MODULE]_form_alter() function. When you see what it looks like, you can see how you can override and add to it as much as you want. To do this, pub the following at the top of your [MODULE]_form_alter() function:

<?php
debug
($form, '$form contents:', TRUE);
?>

Jaypan
Our newest Drupal site: PacificAikido.com (Drupal showcase)

Thanks jay,
I have tried out the above code, but i m getting the same error/notice.
1) Notice: Undefined variable: form in include_once() (line 2 of /var/www/myshaddi/sites/all/modules/patch/patch.module).
2) Notice: Undefined index: mail in user_account_form_validate() (line 1214 of /var/www/myshaddi/modules/user/user.module).
3) Notice: Undefined index: mail in user_account_form_validate() (line 1215 of /var/www/myshaddi/modules/user/user.module).
4) Recoverable fatal error: Argument 2 passed to drupal_array_set_nested_value() must be an array, null given, called in /var/www/myshaddi/includes/form.inc on line 2436 and defined in drupal_array_set_nested_value() (line 6300 of /var/www/myshaddi/includes/common.inc).

Here is my code...

<?php
function patch_form_alter(&$form, &$form_state, $form_id){
    switch(
$form_id) {
      case
'user_register_form': // the value we stole from the rendered form
       
$form['personal'] = array(
       
'#type' => 'fieldset',
       
'#title' => t('Personal information'),
       
'#weight' => 1,
        );
       
$form['personal']['first_name'] = array(
       
'#type' => 'textfield',
       
'#title' => t('First Name'),
       
'#weight' => 4,
        );
       
$form['personal']['last_name'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Last Name'),
       
'#weight' => 5,
        );
       
$form['personal']['email'] = array(
       
'#type' => 'textfield',
       
'#title' => t('E-mail address'),
       
'#weight' => 1,
        );
       
$form['personal']['password'] = array(
       
'#type'=>'password',
       
'#title'=>'Password',
       
'#weight'=>'2',
        );
       
$form['personal']['created_for'] = array(
       
'#type'=>'select',
       
'#options' => array(
                           
''=>'Profile Created For',
                           
'Self'=>'Self',
                           
'Son'=>'Son',
                           
'Daughter'=>'Daughter',
                           
'Brother'=>'Brother',
                           
'Sister'=>'Sister',
                           
'Friend'=>'Friend',
                           
'Relative'=>'Relative'
                           
),
       
'#title' =>'Profile Created For',
       
'#weight' => '3',
       
'#default_value'=>'',
        );
       
$form['more_info_about_you'] = array(
       
'#type'=>'fieldset',
       
'#title'=>'More information about you',
       
'#weight'=>6
       
);
       
$form['more_info_about_you']['gender'] = array(
       
'#type'=>'radios',
       
'#title'=>'Gender',
       
'#options'=>array('male'=>'Male','female'=>'Female'),
       
'#default_value'=>'male',
        );
       
$day[]='Day';
        for(
$i=1;$i<=31;$i++){
           
$day[$i]=$i;
        }
           
$month = array(
                       
''=>'Month',
                       
'1'=>'Jan',
                       
'2'=>'Feb',
                       
'3'=>'Mar',
                       
'4'=>'Apr',
                       
'5'=>'May',
                       
'6'=>'Jun',
                       
'7'=>'Jul',
                       
'8'=>'Aug',
                       
'9'=>'Sep',
                       
'10'=>'Oct',
                       
'11'=>'Nov',
                       
'12'=>'Dec',
                        );
       
$year[]='Year';
        for(
$i=1970;$i<=date('Y');$i++){
           
$year[$i]=$i;
        }
       
$form['more_info_about_you']['dob'] = array(
           
'#type'=>'select',
           
'#title'=>'DOB',
           
'#options'=>$day,
        );
       
$form['more_info_about_you']['month'] = array(
           
'#type'=>'select',
           
'#options'=>$month,
        );
       
$form['more_info_about_you']['year'] = array(
           
'#type'=>'select',
           
'#options'=>$year,
        );
       
$form['submit'] = array(
           
'#type'=>'submit',
           
'#value'=>'Sumit',
           
'#weight'=>7,
        );
       
$form['#validate'][] = 'my_custom_validate_function';
                
$form['#submit'][] = 'my_custom_submit_function';
         unset(
$form["account"]["name"]);
         unset(
$form["account"]["mail"]);
         unset (
$form['actions']);
         return
$form;
      break;
    } 
}
function
my_custom_validate_function($form, &$form_state)
{
   echo
'Test-validate';
}
function
my_custom_submit_function($form, &$form_state)
{
     echo
'Test-submit';
}
?>

But the validate and submit function is not calling from form alter hook.

Thanks
drupsforyou

1) Please wrap your code in php tags, it's too hard to read the way you've done it.
2) You don't return anything from hook_form_alter, both $form and $form_state are passed by reference.
3) Your first error most likely doesn't have anything to do with the code you've shown. It says the problem is coming from line 2 of your module, which I'm guessing isn't shown, since it appears you are trying to include a file in line 2 and it's not working.
4) Your other problems may be a result of echoing values in your validate and submit statements. Try using debug('validate function'); and debug('submit_function'); instead.

Jaypan
Our newest Drupal site: PacificAikido.com (Drupal showcase)

I have tried out the below code, but till now it's not working .
Module name : patch.module // Custom module

<?php
function patch_form_alter(&$form, &$form_state, $form_id){
    switch(
$form_id) {
      case
'user_register_form': // the value we stole from the rendered form
       
$form['personal'] = array(
       
'#type' => 'fieldset',
       
'#title' => t('Personal information'),
       
'#weight' => 1,
        );
       
$form['personal']['first_name'] = array(
       
'#type' => 'textfield',
       
'#title' => t('First Name'),
       
'#weight' => 4,
        );
       
$form['#validate'][] = 'patch_custom_validate_function';
       
$form['#submit'][] = 'patch_custom_submit_function';
         unset(
$form["account"]["name"]); // unset the default name textfield
        
unset($form["account"]["mail"]);// unset the default email textfield
        
unset ($form['actions']);// unset the default submit Button
        
return $form;
      break;
    }
}
function
patch_custom_validate_function($form, &$form_state)
{
   echo
'Test-validate';
}
function
patch_custom_submit_function($form, &$form_state)
{
     echo
'Test-submit';
}
?>

Thanks
drupsforyou

Ok, that covered point #1 that I made earlier. Now please refer to points 2, 3 and 4.

Point two was unclear though, it should have read:

'Don't return anything from hook_form_alter'. I didn't mean to say that you aren't doing it, I meant to say that you are doing it and shouldn't.

Jaypan
Our newest Drupal site: PacificAikido.com (Drupal showcase)

Thank to all,
Finally i got the solution. Thanks you once for your reply.

Thanks
drupsforyou

Those calls to unset are causing you problems since they are removing information the standard validate and submit expect.

good example

It is always nice to post the exact solution so others not so clever on the subject could learn from it. Thanks!

Don't keep it to yourself, share the solution.

PIÑA&POLLO