Tagged: custom field, unique, validation
- This topic has 9 replies, 2 voices, and was last updated 6 years, 11 months ago by
Steve.
-
AuthorPosts
-
-
February 19, 2015 at 4:54 pm #3326
justinMemberI 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' ) ) )); -
February 21, 2015 at 1:30 am #3332
-
February 23, 2015 at 10:20 am #3341
justinMemberI 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');} } -
February 23, 2015 at 3:38 pm #3345
SteveKeymaster@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'); } } -
February 23, 2015 at 5:40 pm #3348
justinMemberHi,
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'); } } -
February 23, 2015 at 5:56 pm #3349
-
February 24, 2015 at 8:53 am #3352
justinMemberHi 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'); } } -
February 24, 2015 at 10:37 am #3356
SteveKeymaster@justin– If you can’t
print_rthe$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.phpfile.Feel free to zip up your entire plugin/theme and email to [email protected] if you’re still having issues.
-
February 24, 2015 at 1:54 pm #3359
justinMemberSteve,
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'); } } -
February 25, 2015 at 12:29 pm #3360
SteveKeymasterGlad it works… and thanks for posting the code snippet! Closing this ticket.
-
-
AuthorPosts
- The topic ‘Validate Unique value’ is closed to new replies.