views:

49

answers:

1

Hey all,

I'm doing some webform validation using webform_form_alter. I'm using webform_form_alter because I switch certain content on a "select" field.

In my webform-form-317.tpl.php I defined new fieldsets I set my fields into this new fieldset and unset the original from the webform.

$form['submitted']['ContactInfo'] = array(
'#type' => 'fieldset',
'#prefix' => '<div id="ContactInfo">',
'#suffix' => '</div>',
'#weight' => -10,
'#title' => 'Contact Information'
);

$form['submitted']['ContactInfo']['phone_home'] = $form['submitted']['phone_home'];
unset($form['submitted']['phone_home']);

in my webform_form_alter I have the following code:

switch ($form_id)
{
case 'webform_client_form_317':
{
$form['#validate'][] = 'validate_form';
}
}

My Validate_form function looks like:

function validate_form($form_id, $form_values)
{
      if ($form_values['submitted_tree']['ContactInfo']['phone_home'] == "")
      {
             form_set_error('phone_error', t('Please enter a home phone number.'));
      }
}

The issue is that the $form_values['submitted_tree']['ContactInfo']['phone_home'] comes back as nothing even is the user has inputted something into the textfield.

Any suggestions on what i'm doing wrong?

As a second question in case somebody also the answers, how do I modify the of these textfields to set the class for "form-text required error" so they show up in red with the rest of the mandatory fields.

Thanks

A: 

I hope you don't write this code in the webform module, but have made your a custom module for it.

In the first case, your function should be

function validate_form($form, &$form_state) {
    if ($form_state['values']['submitted_tree']['ContactInfo']['phone_home'] == "") {
         form_set_error('phone_home', t('Please enter a home phone number.'));
    }
}

If you are talking about the error class, Drupal add it to all fields that has an error set like is done on the above code. You need to pass in the name of the form field as first param to the form_set_error function.

googletorp
Hey googletorp. That unfortunately still doesn't give me the value from the 'phone_home' even if the field has text in it (or doesnt), I always get the "Please enter a home phone number error" (in other words the $form_state[...] is always "" (or null)). If I do a drupal_set_message on that value for fun and I get nothing.
John Ruppet
Got it using your answer here http://stackoverflow.com/questions/1464056/drupal-6-form-state-values-empty-on-submit thanks!
John Ruppet