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.
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.
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?
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?
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?
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.
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.
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:
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.
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?
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.
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…