Advertisement
adczk

Formi - check duplicates or not

Mar 27th, 2023
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. <?php
  2.  
  3. add_filter( 'forminator_custom_form_submit_errors', 'check_form_duplicate_submission_data', 99, 3 );
  4. function check_form_duplicate_submission_data( $submit_errors, $form_id, $field_data_array ) {
  5.  
  6. $form_ids = array( 22246 ); // form IDs - one or more; if more - separate numbers with commas
  7. $fields_to_check = array(
  8. 'phone-2' => 'phone-1',
  9. 'email-2' => 'email-1',
  10. ); // field pairs to check - each key-value pair represents a pair of fields to check for duplicates
  11. $field_delete = 'checkbox-1'; // id of "DELETE" field
  12. $err_msg = 'No registration was found with the given data'; // custom error message
  13. $found_duplicate = false;
  14. $check = false;
  15.  
  16. if ( !in_array( $form_id, $form_ids ) ) {
  17. return $submit_errors; // just bail out and skip checks
  18. }
  19.  
  20. foreach ( $fields_to_check as $field_to_check => $field_to_compare ) {
  21. $field_to_check_value = '';
  22. $field_to_compare_value = '';
  23.  
  24. foreach( $field_data_array as $key => $value ) {
  25. $field_name = $value['name'];
  26.  
  27. if ($field_name == $field_delete) {
  28. $check = true;
  29. }
  30.  
  31. if ( $field_name === $field_to_check ) {
  32. $field_to_check_value = $value['value'];
  33. } elseif ( $field_name === $field_to_compare ) {
  34. $field_to_compare_value = $value['value'];
  35. }
  36. }
  37.  
  38. if ($check) {
  39.  
  40. if ( $field_to_check_value && $field_to_compare_value && $field_to_check_value === $field_to_compare_value ) {
  41. $entries = Forminator_Form_Entry_Model::select_count_entries_by_meta_field( $form_id, $field_to_compare, $field_to_compare_value, '*' );
  42.  
  43. if ( $entries ) {
  44. $submit_errors[][$field_to_check] = $err_msg;
  45. $found_duplicate = true;
  46. }
  47. }
  48.  
  49. }
  50. }
  51. if ( !$found_duplicate ) {
  52. $submit_errors[]['general'] = $err_msg;
  53. }
  54.  
  55.  
  56.  
  57. return $submit_errors;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement