This can be a useful bit of code to ensure that two fields are filled in with the same value (such as two email address that must match).

Create two e-mail components with the field keys 'email1' and 'email2'. Then in the "Webform advanced settings" fieldset, enter this code into the Additional validation text field.

  if ($form_values['submitted_tree']['email1'] != $form_values['submitted_tree']['email2']) {
    form_set_error('submitted][email1', t('E-mail addresses must match.'));
  }

Comments

msteudel’s picture

I'm curious in the form_set_error you are missing some of the brackets

'submitted][email1'

Is that how you are supposed to do it? Or is it just a typo?

Phil.Hersh’s picture

Hi msteudel,

Yes, that is the correct syntax. You'd have top ask a Drupal / PHP wiz why, but it works.

Phil

roball’s picture

It's the correct syntax of Drupal's form_set_error function.

-- Robert

rpsu’s picture

As I bounced my head against the wall for quite some time and searched through Drupal-site for an answer I wanted to give a heads up on validation for someone who might need this:

If your custom webform validation seems not to work, check your code carefully elsewhere ie. execute it somewhere. Code errors seems to prevent your validation to be executed and there seems to be no warning or notice -mechanism whatsoever for this. I wandered a lot why my $form_values -variable seems not to present while doing some Additional validationing, but the code just simply was broken. No errors, no warnings.

//ropsu

--
Perttu Ehn

mlaw’s picture

I copied the snippet above exactly, pasted it into webform additional validation, made my field keys 'email1' and 'email2', and it doesn't validate .... any clue?

PinkChocobo’s picture

I'm sure there is a better way to do this, like to putting it in an array or something, but this works to check to make sure 3 fields don't match (change == to != if you want them to match)

 if ($form_values['submitted_tree']['choice_1'] == $form_values['submitted_tree']['choice_2'] or
     $form_values['submitted_tree']['choice_1'] == $form_values['submitted_tree']['choice_3'] or
     $form_values['submitted_tree']['choice_2'] == $form_values['submitted_tree']['choice_3'])
 {

   form_set_error('submitted][choice_1', t('Please provide three different choices'));

 }
PinkChocobo’s picture

Or this

  if ($form_values['submitted_tree']['choice_1'] == $form_values['submitted_tree']['choice_2']) {
    form_set_error('submitted][choice_1', t('Same values not allowed for Preferred choice 1 and choice 2.'));
  }

  if ($form_values['submitted_tree']['choice_1'] == $form_values['submitted_tree']['choice_3']) {
    form_set_error('submitted][choice_1', t('Same values not allowed for choice 1 and choice 3.'));
  }
  
  if ($form_values['submitted_tree']['choice_2'] == $form_values['submitted_tree']['choice_3']) {
    form_set_error('submitted][choice_2', t('Same values not allowed for choice 2 and choice 3.'));
  }
     
PinkChocobo’s picture

or this

 if ($form_values['submitted_tree']['choice_1'] == $form_values['submitted_tree']['choice_2']) {
       form_set_error('submitted][choice_1', t('choice 1 must be different from choice 2'));

} elseif($form_values['submitted_tree']['choice_1'] == $form_values['submitted_tree']['choice_3']) {
       form_set_error('submitted][choice_1', t('choice 1 must be different from choice 2'));

} elseif($form_values['submitted_tree']['choice_2'] == $form_values['submitted_tree']['choice_3']) {
       form_set_error('submitted][choice_2', t('choice 2 must be different from choice 3'));
}
opteronmx’s picture

For D6 $form_values must change to $form_state['values'], as follows:

  if ($form_values['values']['submitted_tree']['email1'] != $form_values['values']['submitted_tree']['email2']) {
    form_set_error('submitted][email1', t('E-mail addresses must match.'));
  }
swr’s picture

In your example, opteronmx, you haven't changed the $form_values to $form_state. (You got the ['values'] in!) It should look like this:

<?php
  if ($form_state['values']['submitted_tree']['email1'] != 
$form_state['values']['submitted_tree']['email2']) {
    form_set_error('submitted][email1', t('E-mail addresses must match.'));
  }
?>

This does appear to work with D6.

opteronmx’s picture

Oops, yes, I forgot to change that, thank you swr

mickster917’s picture

This still is not working for me. Using Drupal 6.19 and Webform 6.x-2.9. I tried submitting the form using test@test for one email and testing@testing for the other. I didn't get any errors or notification that the fields did not match. Any suggestions?

mickster917’s picture

forgot to exclude the field in FCKeditor before putting in the php code.

bbalasateesh’s picture

I have installed webform-6.x - 3.2. I want the values in two email fields to be matched. I didn't have the option "Additional validation text field" in the Advanced Settings. Please let me know how can I do this validation.

Thanks in advance.
Sateesh B.

rpsu’s picture

Additional validation is moved elsewhere in 6.x-3.x -branch.
See:
http://drupal.org/project/webform_validation -- input validation through some simple rules (numeric, max/min length etc.)
http://drupal.org/project/webform_php -- previous Additional Validation has moved here

--
Perttu Ehn