Skip to content

Instantly share code, notes, and snippets.

@askaryabbas
Last active November 16, 2017 19:01
Show Gist options
  • Save askaryabbas/71d77de0d1b7a680a69d525f5bbc80e1 to your computer and use it in GitHub Desktop.
Save askaryabbas/71d77de0d1b7a680a69d525f5bbc80e1 to your computer and use it in GitHub Desktop.
add_filter( 'gform_validation', 'ask_custom_validation' );
function ask_custom_validation( $validation_result ) {
global $email, $wpdb;
$table_lead = $wpdb->prefix . 'rg_lead';
$table_lead_detail = $wpdb->prefix . 'rg_lead_detail';
// new gravity form
$rg = new RGFormsModel();
// grab the form
$form = $validation_result[ "form" ];
$form_id = $form[ 'id' ];
// only validate the vault form
// Please replace form title with your form title
if ( $form[ 'title' ] == "Registration with Paytm Payment" ) {
foreach ( $form[ 'fields' ] as &$field ) {
// store email field in variable
// Please replace form field id with your form field id
if ( $field[ 'id' ] == "2" && $field[ 'type' ] == 'email' ) {
$email = rgpost( "input_{$field[ 'id' ]}" );
}
$field_id = $field[ 'id' ];
// check the uid field
if ( $field[ 'label' ] == "Email" ) {
// search gravity forms for this email
$sql = "SELECT * FROM $table_lead as lead
JOIN $table_lead_detail as lead_detail ON lead.id = lead_detail.lead_id
WHERE lead.form_id=$form_id
AND field_number = $field_id
AND value LIKE '%$email%' AND payment_status = 'Success'";
$result = $wpdb->get_results( $sql, ARRAY_A );
// if email already exists
if ( count( $result ) > 0 ) {
// set the form validation to false
$validation_result[ "is_valid" ] = false;
// specify the first field to be invalid and provide custom validation message
$form[ "fields" ][ 1 ][ "failed_validation" ] = true;
$form[ "fields" ][ 1 ][ "validation_message" ] = __( 'This email is already registered!', 'text_domain' );
// update the form in the validation result with the form object you modified
$validation_result[ "form" ] = $form;
}
}
}
}
return $validation_result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment