Gravity Forms - Compare value against another field

2.3k views Asked by At

Hoping someone can shed some light on this. I have a registration form setup in Gravity Forms for the registration of students. According to law a students guardian/parent needs to give consent for students under the age of 18. I have setup a conditional "Guardian/Parent" email field that only shows if the student is under the age of 18.

So X2 email fields are present in the form, one for the student and one for the guardian/parent. Once the forms is submitted, the activation mail is sent only to the guardian email address.

My problem is that at the moment the same email address can be pout in both the guardian/parent and student email. I would like to validate the 2 email fields and ensure that they are not the same.

I tried the following, but it has no effect at all.

add_filter( 'gform_field_validation_12_3', function ( $result, $value, $form, $field ) {
    $master = rgpost( 'input_11' );
    if ( $result['is_valid'] && $value == $master ) {
        $result['is_valid'] = false;
        $result['message']  = 'Please enter a different email.';
    }

    return $result;
}, 10, 4 );

The "12" in "gform_field_validation_12_3" represent my form_id and the "3" represents the guardian/parent field_id

The "11" in "$master = rgpost( 'input_11' );" represents my student email field_id

It simple does not validate the fields and submits the form even though I have the same email in both fields....

1

There are 1 answers

0
Dave from Gravity Wiz On

If email confirmation is enabled, you'll want to change this line:

if ( $result['is_valid'] && $value == $master ) {

to this:

if ( $result['is_valid'] && $value[0] == $master ) {

The issue is that when email confirmation is enabled, the $value is an array not a string. You can just get the first value from the array and it should work as expected.

If you don't want to fuss with having to know all of this, feel free to use this snippet I just wrote. It will also handle multiple fields and multiple groups of "unique" fields.

http://gravitywiz.com/gravity-forms-require-unique-values-for-different-fields/