Support

Account

Home Forums Front-end Issues Accept only unique values

Solving

Accept only unique values

  • Hi,

    This is perfect to working. Thank you for this post 🙂 . I have one doubt because one of the error occurring my site. I want only validation in new post create. No need for update post page. How do to fix it?. Please help us. Thanks! This is Really Great!!!. Thank you

    Regards,

    Gayathri

  • I’m not 100% sure how you’d do that. You might use the value of $post_id and check the status of the post. There might also be other values in $_POST that you can check. It would take some testing.

  • Hi all!

    I’m trying to use the code with a telephone field called ‘phone’ but it doesn’t work. This is the code I insert in functions php.
    What I’m doing wrong?

    Thx.

    add_filter('acf/validate_value/name='.$field_name, 'acf_unique_value_field', 10, 4);
      
      function acf_unique_value_field($valid, $value, $field, $input) {
        if (!$valid || (!isset($_POST['post_ID']) && !isset($_POST['post_id']))) {
          return $valid;
        }
        if (isset($_POST['post_ID'])) {
          $post_id = intval($_POST['post_ID']);
        } else {
          $post_id = intval($_POST['post_id']);
        }
        if (!$post_id) {
          return $valid;
        }
        $post_type = get_post_type($post_id);
        $field_name = $field['phone'];
        $args = array(
          'post_type' => $post_type,
          'post_status' => 'publish, draft, trash',
          'post__not_in' => array($post_id),
          'meta_query' => array(
            array(
              'key' => $field_name,
              'value' => $value
            )
          )
        );
        $query = new WP_Query($args);
        if (count($query->posts)){
          return 'This Value is not Unique. Please enter a unique '.$field['label'];
        }
        return true;
      }
  • 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.

  • I am following the snippet provided by John on Nov 2016 and I can get an error log from within the final ‘if’ before returning “This Value is not Unique”. However,
    1. that error message does not show up on the custom post page while I am editing it.
    2. the post gets saved

    Can someone help?

    I’ve implemented the function using the ‘snippets’ plugin and my backend editor is gutenberg

    Cheers,
    Karthik

  • The solution proposed by John Huebner to Accept Only Unique Values is working fine in the backend.

    But I made a frontend form using Elementor form and the error message of non unique value do not appear in the frontend form and also the post is saved in db even with duplicate value.

    I am using Elementor Pro, ACF Pro and the ActionsPack plugin (Save action).

    Does anyone knows a solution to this problem ?

    Thnaks.

Viewing 6 posts - 26 through 31 (of 31 total)

The topic ‘Accept only unique values’ is closed to new replies.