Viewing 9 reply threads
  • Author
    Posts
    • #3326
      justin
      Member

      I was wondering how you could enforce a unique value for a text field.
      I am allowing each user to enter in their custom URL to take a user to their profile, but I need to be able to validate the field that it is not currently being used.
      Currently the below code allows unique values.

      
      
      add_filter('piklist_validation_rules', 'check_if_unique', 11);
      function check_if_unique($validation_rules)
      {
        $validation_rules['custom_unique'] = array(
          'callback' => 'my_validate_unique'
        );
       
        return $validation_rules;
      }
       
      function my_validate_unique($file, $field, $arguments)
      {
      	$user_query = new WP_User_Query( array( 'meta_key' => 'user_custom_url', 'meta_value' => $field ) );
      	if( !empty ($user_query)){
      		return true;
      	}else{ return false;}
       
      }
      

      This is my field:

      
       piklist('field', array(
       'type' => 'text'
       ,'field' => 'user_custom_url'
       ,'label' => 'URL of your profile'
       ,'validate' => array(
        array(
          'type' => 'custom_unique'
        )
      )
       ));
      
    • #3332
      Steve
      Keymaster

      @justin

      Instead of returning false, you should return a message:

      return __('is not unique', 'text-domain');
      

      Let us know if that works.

    • #3341
      justin
      Member

      I saved two separate user accounts to have the same field, and it did not popup an error. I wonder if I am putting the add_filter in the wrong place. I placed it at the bottom of my plugin main php page. I also tried putting it only on the bottom of the users field php page which did not work either.

      
      add_filter('piklist_validation_rules', 'check_if_unique', 11);
      function check_if_unique($validation_rules)
      {
        $validation_rules['custom_unique'] = array(
          'callback' => 'my_validate_unique'
        );
       
        return $validation_rules;
      }
       
      function my_validate_unique($file, $field, $arguments)
      {
      	$user_query = new WP_User_Query( array( 'meta_key' => 'user_custom_url', 'meta_value' => $field ) );
      	if( !empty ($user_query)){
      		return true;
      	}else{ return __('is not unique', 'text-domain');}
       
      }
    • #3345
      Steve
      Keymaster

      @justin– I think the usage of WP_User_Query is off. Use this post by Tom Mcfarlin for reference >

      Try this. It worked for me:

      add_filter('piklist_validation_rules', 'check_if_unique', 11);
      function check_if_unique($validation_rules)
      {
        $validation_rules['custom_unique'] = array(
          'callback' => 'my_validate_unique'
        );
       
        return $validation_rules;
      }
       
      function my_validate_unique($file, $field, $arguments)
      {
        $user_query = new WP_User_Query( array( 'meta_key' => 'user_custom_url', 'meta_value' => $field) );
      
        $users = $user_query->get_results();
      
        if(empty($users[0]))
        {
          return true;
        }
        else
        { 
          return __('is not unique', 'your-text-domain');
        }
       
      }
      
    • #3348
      justin
      Member

      Hi,

      Is the “$field” an array? When I hard-code the field value into the meta_value query, i can get the correct result, but when i use “$field” to get the current field value, it never says that the field has a duplicate.

      Here is the modified query that works if you remove “$field” and replace it with a value that you want to test against.

      
      add_filter('piklist_validation_rules', 'check_if_unique', 11);
      function check_if_unique($validation_rules)
      {
        $validation_rules['custom_unique'] = array(
          'callback' => 'my_validate_unique'
        );
       
        return $validation_rules;
      }
       
      function my_validate_unique($file, $field, $arguments)
      {
      	global $user_id;
      $the_user_id= $user_id;
        $user_query = new WP_User_Query( array( 'meta_key' => 'user_custom_url', 'meta_value' => $field, 'exclude' =>$the_user_id) );
        $users = $user_query->get_results();
      $userscount = $user_query->get_total();
        if($userscount != 1 )
        {
          return true;
        }
        else
        { 
          return __('is not unique', 'your-text-domain');
        }
       
      }
    • #3349
      Steve
      Keymaster

      @justin– Yup, it is! $field['value'][0]

      If you include print_r($field); in your my_validate_unique function you can see everything that gets returned.

    • #3352
      justin
      Member

      Hi Steve,

      I am sorry, but no mater what I try I can not get it to work. I must be missing something. I can not even get it to print_r($field), or print_r(‘test’) to work inside that function. I have now tried both $field[‘value’] and $field[‘value’][0] to no luck. With either of those every time it comes up valid. When I replace the variable with a physical string, I am able to validate the field correctly.

      Just for reference I have updated to the latest beta today and still have the problem: 0.9.5

      
      add_filter('piklist_validation_rules', 'check_if_unique', 11);
      function check_if_unique($validation_rules)
      {
        $validation_rules['custom_unique'] = array(
          'callback' => 'my_validate_unique'
        );
       
        return $validation_rules;
      }
       
      function my_validate_unique($file, $field, $arguments)
      {
      	print_r($field); //I can not find this printing anywhere on the page
      	global $user_id;
      	$user_profile_id = $user_id;
      	$user_query = new WP_User_Query( 
      	array( 
      		'meta_key' => 'user_custom_url'
      		,'meta_value' => $field['value'] //Works if this is replaced with string - ie 'value'
      		,'exclude' =>$user_profile_id
      		) 
      	);
      $userscount = $user_query->get_total();
        if($userscount != 1 )
        {
        return true;
        }else{
      	  return __('is not unique', 'your-text-domain');
        }
       
      }
      
    • #3356
      Steve
      Keymaster

      @justin– If you can’t print_r the $field, then there’s definitely an issue! 😉

      Where are you placing this code? It should be in your custom plugins main file, or your theme’s functions.php file.

      Feel free to zip up your entire plugin/theme and email to [email protected] if you’re still having issues.

    • #3359
      justin
      Member

      Steve,

      I finally figured out why this was not working. It had to do with a plugin called php console which prevented me from seeing an error that the piklist toolbox was causing. I find that if i uninstall both php console and the toolbox, that it works, and there are no errors. Thank you so much for helping.

      (just for information on this support issue,
      https://piklist.com/support/topic/notice-trying-to-get-property-of-non-object/
      Those notices are caused by the toolbox plugin. I somehow deactivated it, is why I thought that it was solved.)

      You can close this ticket now.

      Below is code that works for anyone else that is interested.

      add_filter('piklist_validation_rules', 'check_if_unique', 11);
      function check_if_unique($validation_rules)
      {
        $validation_rules['custom_unique'] = array(
          'callback' => 'my_validate_unique'
        );
       
        return $validation_rules;
      }
       
      function my_validate_unique($file, $field, $arguments)
      {
      	global $user_id;
      
      	$user_profile_id = $user_id;
      
      	$user_query = new WP_User_Query( 
      		array( 
      			'meta_key' => 'user_custom_url'
      			,'meta_value' => $field //Works if this is replaced with 'value'
      			,'exclude' =>$user_profile_id
      		) 
      	);
      
      $userscount = $user_query->get_total();
      
        if($userscount != 1 )
        {
        return true;
        }else{
      	  return __('is not unique', 'your-text-domain');
        }
       
      }
      
    • #3360
      Steve
      Keymaster

      Glad it works… and thanks for posting the code snippet! Closing this ticket.

Viewing 9 reply threads
  • The topic ‘Validate Unique value’ is closed to new replies.