Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Code error?
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?
Hi msteudel, Yes, that is
Hi msteudel,
Yes, that is the correct syntax. You'd have top ask a Drupal / PHP wiz why, but it works.
Phil
form_set_error
It's the correct syntax of Drupal's form_set_error function.
-- Robert
Validation failed? Check your code...
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
I copied the snippet above
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?
check multiple
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)
separated
Or this
only do if previous returns false
or this
Ensure Two of the Same Value for D6
For D6 $form_values must change to $form_state['values'], as follows:
code correction
In your example, opteronmx, you haven't changed the $form_values to $form_state. (You got the ['values'] in!) It should look like this:
This does appear to work with D6.
Oops, yes, I forgot to change
Oops, yes, I forgot to change that, thank you swr
grrr
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?
Nevermind
forgot to exclude the field in FCKeditor before putting in the php code.
Dont have option
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.
Additional validation is
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