Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By drupsforyou on
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
You add them to the #validate and #submit arrays:
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:
Re:
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...
But the validate and submit function is not calling from form alter hook.
1) Please wrap your code in
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.
I have tried.
I have tried out the below code, but till now it's not working .
Module name : patch.module // Custom module
Ok, that covered point #1
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.
I got the solution
Thank to all,
Finally i got the solution. Thanks you once for your reply.
Those calls to unset are
Those calls to unset are causing you problems since they are removing information the standard validate and submit expect.
good example
good example
What is the solution?
It is always nice to post the exact solution so others not so clever on the subject could learn from it. Thanks!
share it
Don't keep it to yourself, share the solution.
THE VERY LITTLE AGENCY