I am trying to modify some Drupal (6) form code and incorporate some native form validation. Form is working, but validation does not work. It never even gets into function thisFormName_form_validate. I am not sure if I should be using hook_validate or hook_form_validate. Any Drupalians have some tips for me? Much obliged!
function thisFormName_form_alter(&$form, $form_state, $form_id) {
$form['email_address'] = array(
'#type' => 'textfield',
'#title' => t('Enter your email address (optional)'),
'#default_value' => $object['email_address'],
'#weight' => 4,
'#size' => 60,
'#maxlength' => 128,
'#description' => t('Enter email address.'),
);
function thisFormName_form_validate($node, &$form) {
if ($form_state['values']['email_address'] == '')
{
form_set_error('', t('Email must be valid format if entered.'));
}
}
Comments
http://api.drupal.org/api/dru
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
you can also just set required to true
If you are just wanting to make sure the email field is not empty, set required to true like:
if you do indeed to need to use your own custom validation function, pass it in an array using the #validate key:
Finally, this is an awesome resource
http://api.drupal.org/api/drupal/developer--topics--forms_api.html/6
form submit, validation and re-direct on successful submit
Great advice. I have moved forward (although not very quickly).
The validation was getting more complicated, so I put it all in _validate function.
Now I think that I want to alter form when user has successfully
submitted form to give user a thank-you message, however $form['#redirect']
is redirecting all the time and by-passing SQL update.
Is there a way of detecting error from form_set_error so I can condition
the re-direct? Or maybe a better strategy that I have missed.
thanks again!
Why do you want to have
Why do you want to have hook_form_alter in your own custom form? unless you want to edit "other" module form then I believe you should use _form_submit() instead of hook_form_alter.
--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com
I have the same problem
I have the same problem but I cannot resolve it. I have very simple module with very simple form
I display it in node with function
echo drupal_get_form('myform');
in node.tpl.php
the form displays but validate function does not run after submission
Anybody can help?
Well, there's your problem
This isn't the way you display forms in Drupal. Instead, you define a menu item, with the
page callback
key set to 'drupal_get_form,' and thepage arguments
key set to an array of form generation functions. If you're making a node type, you can also overridehook_form()
.But you can't just stick them on a page somewhere and expect them to work.
For further reading:
http://api.drupal.org/api/group/menu
http://api.drupal.org/api/function/hook_form
-Karlheinz
thank you I am going to study
thank you
I am going to study it
Tomas