Support

Account

Home Forums General Issues Validate Front End Form Submission – Unique Values only Reply To: Validate Front End Form Submission – Unique Values only

  • Hi John,

    Thanks for the pointers – I have added the above to functions.php and edited to add in the relevant field keys but am still not having any luck.

    The validation works if I add in an OR relation so looks like the vacancy applied for side of things is working but the candidate email value doesn’t seem to be working.

    Tried also swapping things around to have it check against the candidate_email field and works again if I use an OR relation but not with the AND.

    // Validate application is not a duplicate
    add_filter('acf/validate_value/key=field_5dba152669aaf', 'acf_duplicate_candidate_application', 10, 4);
    
    function acf_duplicate_candidate_application($valid, $value, $field, $input) {
      // acf now uses $_POST['_acf_post_id']
      // the other $_POST indexes may not be available on the front end
      if (!$valid || !isset($_POST['_acf_post_id'])) {
        return $valid;
      }
      $post_id = intval($_POST['_acf_post_id']);
      $post_type = get_post_type($post_id);
      
      // to get other values submitted you must look in $_POST['acf'][$field_key]
      $candidate_email = $_POST['acf']['field_5db9db5da28c0']; // field key of email field
      
      $args = array(
        'post_type' => 'candidates',
        'post_status' => 'publish', // double check possible post statuses 
        'post__not_in' => array($post_id),
        'meta_query' => array(
          'relation' => 'AND',
          array(
            'key' => 'candidate_email', // field name
            'value' => $candidate_email
          ),
          array(
            'key' => 'vacancy_applied_for', // field name
            'value' => $value
          )
        )
      );
      $query = new WP_Query($args);
      if (count($query->posts)){
        return 'You have already applied for this post.  Please content OHSC HR via email to update your application if you want to submit additional information.  Alternatively you can apply for other vacancies <a href="#">here</a>';
      }
      return true;
    }
    

    Here is the current code snippet. Not sure what the issue is.