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

justageek’s picture

If you are just wanting to make sure the email field is not empty, set required to true like:

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.'),
'#required' => TRUE
);

if you do indeed to need to use your own custom validation function, pass it in an array using the #validate key:

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.'),
'#validate' => array('my_validate_function_name_here')
);

Finally, this is an awesome resource

http://api.drupal.org/api/drupal/developer--topics--forms_api.html/6

itp’s picture

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!


// hook_form
function my_third_module_form() {
  return drupal_get_form('my_form');
}

// define form
function my_form($form_state) {

  // Email address  
  $form['email_address'] = array(
    '#type' => 'textfield',
    '#title' => t('Your email address  (optional)'),
    '#default_value' => $object['email_address'],
    '#weight' => 3,
    '#size' => 60,
    '#maxlength' => 128,  
    // 	'#validate' => array('check_my_email_address'),  I took this out because validation got complicated..
    '#description' => t('Enter email address if you want a reply to your question or a copy of your question.'),
  );

   // and so on...  
  
  return $form;
}


// hook_validate 
function my_form_validate($node, &$form) {

	// get varaibles from form
	$question = $form['values']['title'];
	$send_copy = $form['values']['send_copy'] ;
	$emailAddress = $form['values']['email_address'];
    
    // validate Email address must be valid if entered
    if ($emailAddress != '') 
    {
        if (check_my_email_address($emailAddress) == FALSE)
        {
            form_set_error('', t('Email, if entered, must be in valid format'));
        }
    }
    
    // If Send copy is requested, then must enter email address
    if ($send_copy == '1') 
    {
        if ($emailAddress == '') {
            form_set_error('', t('Email address is required if you want a copy of question'));
        }        
    }

	// and so on...

}
	 

// hook_form_alter
function my_third_module_form_alter(&$form, $form_state, $form_id) {
	// here I think that I want to alter form when 
        // user has successfully submitted form, however $form['#redirect']
        // is redirecting all the time and by-passing SQL update.
        // Is there a way of detecting error from form_set_error?

	// $form['#redirect'] = 'thank_you_for_submission';

}
duckzland’s picture

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

tomas.teicher’s picture

I have the same problem but I cannot resolve it. I have very simple module with very simple form



function myform($form_state) {


  $form['surname'] = array(
    '#type' => 'textfield',
    '#title' => t('surname'),
  );

$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  return $form;
}

function myform_validate($node, &$form) { //I also tried parameters ($form, &$form_state)
 form_set_error('', t('test validation')); //I tried also simple: echo 'validation'; and nothing displayed
   }

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?

Karlheinz’s picture

I display it in node

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 the page arguments key set to an array of form generation functions. If you're making a node type, you can also override hook_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

tomas.teicher’s picture

thank you
I am going to study it
Tomas