Skip to content

Instantly share code, notes, and snippets.

@mandiwise
Last active January 27, 2017 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandiwise/10022539dc8333949e21 to your computer and use it in GitHub Desktop.
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)
<?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