Support

Account

Home Forums Front-end Issues Accept only unique values Reply To: Accept only unique values

  • Hi guys,

    I am trying to validate uniqueness based on two fields of a front-end form. I want the combination of the two fields to be unique and to show a user a validation error if not unique.

    I have the following two fields setup ACF:
    candidate_email
    vacancy_applied_for

    I have setup the following code but the validation message is not working.

    function my_acf_validate_value( $valid, $value, $field, $input_name ) {
    
        // Bail early if value is already invalid.
        if( $valid !== true ) {
            return $valid;
        }
    
        $args = array(
          'post_type' => 'candidates',
          'post_status' => 'publish, draft, trash',
          'post__not_in' => array($post_id),
          'meta_query' => array(
            'relation' => 'AND',
            array(
              'key' => 'candidate_email',
              'value' => $value
            ),
            array(
              'key' => 'vacancy_applied_for',
              '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 $valid;
    }
    
    add_filter('acf/validate_value/name=vacancy_applied_for', 'my_acf_validate_value', 10, 4);

    Please help! When I set the relation query to an OR then the validation works.