Last active
January 27, 2017 21:27
-
-
Save mandiwise/10022539dc8333949e21 to your computer and use it in GitHub Desktop.
Prevent a given form submitter from entering the same value in a form field twice (on multiple submits)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function no_duplicate_votes($validation_result){ | |
global $wpdb; | |
$nominee_field = 1; | |
$email_field = 2; | |
$form = $validation_result['form']; | |
foreach( $form["fields"] as &$field ) { | |
if ( $field['id'] == $email_field ) { | |
$email_value = rgpost("input_{$field['id']}"); | |
$vote_submissions = $wpdb->get_results( $wpdb->prepare( | |
"SELECT lead_id FROM {$wpdb->prefix}rg_lead_detail | |
WHERE form_id = %d and field_number = %d and value = %s | |
ORDER BY id ASC LIMIT 999999", $form['id'], $field['id'], $email_value ) | |
); | |
if ( !empty( $vote_submissions ) ) { | |
foreach ( $vote_submissions as $vote_submission ){ | |
$last_vote = $wpdb->get_var( $wpdb->prepare( | |
"SELECT value FROM {$wpdb->prefix}rg_lead_detail | |
WHERE lead_id = %d and field_number = %d", $vote_submission->lead_id, $nominee_field ) | |
); | |
if ( !empty( $last_vote ) ) { | |
$nominee_value = $_POST['input_1']; | |
if ( $last_vote == $nominee_value ) { | |
$validation_result['is_valid'] = false; | |
$field['failed_validation'] = true; | |
$field['validation_message'] = 'Sorry! You already voted for ' .$nominee_value. ' in this category.'; | |
continue; | |
} | |
} | |
} | |
} | |
} | |
} | |
$validation_result["form"] = $form; | |
return $validation_result; | |
} | |
add_filter( 'gform_validation_[form ID]', 'no_duplicate_votes' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment