Conditional logic with a data set

Hi,

I have a client that is looking for a form to accomplish the following, and was wondering if it was possible with Gravity Forms and the best way to accomplish it. They are looking to have a field that would be matched against information from an excel sheet or some other file. If there is a match, the form would be able to be submitted. For example, there is a field for and address > if the address is not listed in this excel file they won’t be able to submit the form. If it is they can then submit.

Any help would be greatly appreciated.

Thanks in advance.

You can use the gform_field_validation filter for that.

That runs when the form is first submitted. At that time you can compare the value they entered to your data source (plain text or database table are probably easiest) and let the submission through if there is a match, or return a validation error if there is no match.

Hi Chris,

Thanks for your response. I found a previous thread that was trying to accomplish something similar, except with codes rather than text. This is the thread: http://www.gravityhelp.com/forums/topic/database-look-up-validation. Would I be able to adjust this code to fit my needs?

Thanks again!

1 Like

Yes, you can use that approach. Let us know where you get stuck.

Im starting by trying to recreate the code solution, and have copied the code and changed the corresponding spots to fit my form. I keep getting an invalid code error, so I think its something having to do with where my file is. I uploaded the .txt to the media folder on my theme, and copied the full URL including the ‘http:’. Is this the reason its not working? Where should I upload instead?

Thanks

You can use the media folder on your server: that’s fine. Can you share the actual error message with the text of the error, or a screenshot? Also, can you share the URL to the text file on your server?

Sure, no problem.

URL for the text file is:
http://garritonbar.live/wp-content/uploads/2018/11/test.txt

OK, that all looks good. It looks like there is probably an issue with the code. Can you share with us the code you are using, and also let us know the form ID and the field ID of the field in the form. Thank you.

Hi Chris, im using the same snippet of code from that previous thread. The form ID is 2, and the field is 1 for the form im attempting this on.

add_filter('gform_validation_2', 'validate_code');
function validate_code($validation_result){
        if(!is_code_valid($_POST['input_1'])){
        $validation_result['is_valid'] = false;
        foreach($validation_result['form']['fields'] as &$field){
            if($field['id'] == 1){
                $field['failed_validation'] = true;
                                $field['validation_message'] = 'The code you entered is invalid: please try again.';
                break;
            }
        }
    }
    return $validation_result;
}

function is_code_valid($thiscode){
        $codes = file('http://garritonbar.live/wp-content/uploads/2018/11/test.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        foreach($codes as $code){
                if($thiscode == $code){
                        return TRUE;
                }
        }
        return FALSE;
}
 
add_filter('gform_confirmation_2', 'valid_invitation_confirmation', 10, 4);
function valid_invitation_confirmation($confirmation, $form, $lead, $ajax){
        $success_url = get_bloginfo('url') . '/?code=' . $lead[1];
        $confirmation = array('redirect' => $success_url);
        return $confirmation;
}

Thanks again.

Hi, should I open this in a support ticket?

Thanks in advance.

I took a look at the code and added some logging statements. That will help us figure out where the failure occurs. Here is the new code:

add_filter( 'gform_validation_2', 'validate_code' );
function validate_code( $validation_result ){
	// add some logging Chris Hajer Gravity Forms Nov 16, 2018
	GFCommon::log_debug( __METHOD__ . '(): The POST => ' . print_r( $_POST, true ) );
	if( !is_code_valid($_POST['input_1'] ) ){
		GFCommon::log_debug( __METHOD__ . "(): Looks like the code {$_POST['input_1']} was not in the file." );
		$validation_result['is_valid'] = false;
		foreach( $validation_result['form']['fields'] as &$field ){
			if( $field['id'] == 1 ){ // mark field 1 as validation failed
				$field['failed_validation'] = true;
				$field['validation_message'] = 'The code you entered is invalid: please try again.';
				break;
			}
		}
	}
	return $validation_result;
}
// function to check to see if entered code is in the file
function is_code_valid( $thiscode ){
	GFCommon::log_debug( __METHOD__ . "(): The value that was submitted is {$thiscode}." );
	$codes = file( 'http://garritonbar.live/wp-content/uploads/2018/11/test.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
	foreach( $codes as $code ){
		GFCommon::log_debug( __METHOD__ . "(): Submitted: {$thiscode}; From file:{$code} ." );
		if( $thiscode == $code ){
			GFCommon::log_debug( __METHOD__ . '(): We have a match!' );
			return TRUE;
		}
	}
	return FALSE;
}

// did not look at this confirmation code
add_filter( 'gform_confirmation_2', 'valid_invitation_confirmation', 10, 4 );
function valid_invitation_confirmation( $confirmation, $form, $lead, $ajax ){
		$success_url = get_bloginfo('url') . '/?code=' . $lead[1];
		$confirmation = array( 'redirect' => $success_url );
		return $confirmation;
}

For the additional logging to be useful, you will need to activate logging of all messages for Gravity Forms Core on the Forms > Settings page and then Forms > Settings > Logging page.

Awesome - here are the results from after adding the logging you placed in the code:

2018-11-17 4:13:06.812823 - DEBUG --> GFFormDisplay::process_form(): Starting to process form (#2) submission. 
2018-11-17 4:13:06.813565 - DEBUG --> GFFormDisplay::process_form(): Source page number: 1. Target page number: 0. 
2018-11-17 4:13:06.814090 - DEBUG --> validate_code(): The POST => Array
(
    [input_1] => 123456789
    [gform_ajax] => form_id=2&title=&description=&tabindex=1
    [is_submit_2] => 1
    [gform_submit] => 2
    [gform_unique_id] => 
    [state_2] => WyJbXSIsIjhjMTQ0NDFhN2Y2ZTkyZmViMzc1MzkwYTc3MmU5ZGVjIl0=
    [gform_target_page_number_2] => 0
    [gform_source_page_number_2] => 1
    [gform_field_values] => 
)
 
2018-11-17 4:13:06.814146 - DEBUG --> is_code_valid(): The value that was submitted is 123456789. 
2018-11-17 4:13:06.816603 - DEBUG --> is_code_valid(): Submitted: 123456789; From file:123456789
987654321
999999999
 . 
2018-11-17 4:13:06.816658 - DEBUG --> validate_code(): Looks like the code 123456789 was not in the file. 
2018-11-17 4:13:06.816725 - DEBUG --> GFFormDisplay::process_form(): After validation. Is submission valid? No. 
2018-11-17 4:13:15.426996 - DEBUG --> GFCommon::post_to_manager(): endpoint: https://www.gravityhelp.com/wp-content/plugins/gravitymanager/api.php?op=upgrade_message&key=3db3460e26b3eb935a94a6a672797d0d 
2018-11-17 4:13:15.798011 - DEBUG --> GFCommon::log_remote_response(): code: 200; body: <!--GFM-->

From the debugging, it looks like the complete file is being read into the $codes variable, rather than one number per line. I will look into the proper PHP function to split the file on new lines, into an array so you can use this code. If you find the solution before I do please post it here. This is the line that is not working correctly:

$codes = file( 'http://garritonbar.live/wp-content/uploads/2018/11/test.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );

Can you try it with my file here:
http://chrishajer.com/vc/test.txt

It looks like the line endings in your file are different than what I saved with. If you can test the code with this file I think we should be able to fix your file with the codes in it.

Reference for the line ending differences:

Yep, your file worked.

So, I did some digging with how the file was saved based on the link you sent over. I changed the encoding and its good to go!

Im sure ill be back here again at some point while finishing this.

Thank you!

1 Like

Do you know of a way to remove case sensitivity when matching the submission to the .txt? For example, the user enters 123 broad street, but in the file it is listed as 123 Broad Street. How do you make that match regardless of case setting?

It’s really hard to match addresses. Your address “123 Broad Street” could be anything like:

123 broad st
123 broad street
123 broad st
123 BROAD STREET
etc

However, if you really just want to make the match case insensitive, let me know and I’ll look at the code again.

Hello,

this is exactly what we are looking for HOWEVER it doesnt work with version 2.4

used your debug version of thecode above to debug and its not even logging that in the log file.

Simply getting this error:
2021-02-19 1:03:07.145442 - DEBUG → GFFormDisplay::process_form(): Starting to process form (#4) submission.
2021-02-19 1:03:07.146893 - DEBUG → GFFormDisplay::process_form(): Source page number: 1. Target page number: 0.
2021-02-19 1:03:07.148065 - DEBUG → GFFormDisplay::process_form(): After validation. Is submission valid? No.

Tested this on a new form with id of 4 and only the 1 input field… didnt work.

inpecting the input field I can see that the inputs are labelled as:
<input name="input_1" id="input_4_1" type="text" value="" class="medium" aria-invalid="false">

On the frontend it does not even show the failed message just shows the spinner although the log file records the above.

this is the form code:

  • code
        <input type="hidden" class="gform_hidden" name="gform_unique_id" value="">
        <input type="hidden" class="gform_hidden" name="state_4" value="WyJbXSIsIjhiNDM5ZmI5MTQyMWZlNjNkZmU1Njc1ODg3OTVmMzA5Il0=">
        <input type="hidden" class="gform_hidden" name="gform_target_page_number_4" id="gform_target_page_number_4" value="0">
        <input type="hidden" class="gform_hidden" name="gform_source_page_number_4" id="gform_source_page_number_4" value="1">
        <input type="hidden" name="gform_field_values" value="">
        
    </div>
                    </form>
                    </div>

and here is the filter code that I am using:

add_filter( ‘gform_validation_4’, ‘validate_code’ );
function validate_code( validation_result ){ // add some logging Chris Hajer Gravity Forms Nov 16, 2018 GFCommon::log_debug( __METHOD__ . '(): The POST => ' . print_r( _POST, true ) );
if( !is_code_valid(_POST['input_1'] ) ){ GFCommon::log_debug( __METHOD__ . "(): Looks like the code {_POST[‘input_1’]} was not in the file." );
$validation_result[‘is_valid’] = false;
foreach( $validation_result[‘form’][‘fields’] as &$field ){
if( $field[‘id’] == 1 ){ // mark field 1 as validation failed
$field[‘failed_validation’] = true;
$field[‘validation_message’] = ‘The code you entered is invalid: please try again.’;
break;
}
}
}
return $validation_result;
}
// function to check to see if entered code is in the file
function is_code_valid( $thiscode ){
GFCommon::log_debug( METHOD . “(): The value that was submitted is {$thiscode}.” );
$codes = file( ‘https://www.MYDOMAIN.COM/codes.txt’, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
foreach( $codes as $code ){
GFCommon::log_debug( METHOD . “(): Submitted: {$thiscode}; From file:{$code} .” );
if( $thiscode == $code ){
GFCommon::log_debug( METHOD . ‘(): We have a match!’ );
return TRUE;
}
}
return FALSE;
}

I can call the codes.txt file in the browser fine no issues.

Not sure where I am going wrong here. Please can you help out? Paying gravity customer thought I would post here prior to reaching out to support…

Hi Amir. I recommend opening a support ticket for this issue. This solution was posted quite a while ago, and everyone’s use case is different.

Thank you.

Hello Chris,

No problem will open a ticket. Thanks for your input :slight_smile: