Tagged: validation
- This topic has 13 replies, 3 voices, and was last updated 7 years ago by
Steve.
-
AuthorPosts
-
-
January 28, 2015 at 6:21 pm #3237
angeliqueMemberHello,
I’m having trouble with the validation. I’m using it in some setting metaboxes and both of them always pass whatever I feed the textfields with (I tested them separately).
One is a straightforward e-mail metabox:
piklist('field', array( 'type' => 'text' ,'field' => 'email_address' ,'label' => __('Your e-mail address') ,'attributes' => array( 'class' => 'text' ) ,'validate' => array( array( 'type' => 'email' ) ) ));The other one is supposed the check if the input is valid float number:
piklist('field', array( 'type' => 'text' ,'field' => 'tax_rate' ,'label' => __('Tax rate') ,'value' => '' ,'attributes' => array( 'class' => 'text' ) ,'validate' => array( array( 'type' => 'check_valid_float' ) ) ));My custom validation function is like this (if this is important, I put this declaration in the same PHP file that has the setting page declaration):
add_filter('piklist_validation_rules', 'check_valid_float', 11); function check_valid_float() { $validation_rules = array( 'html' => array( 'rule' => "/^[1-9]+(.\d+)?(.\d+)?/" ,'message' => __('is not a valid number.') ) ); return $validation_rules; }I’ve been playing around with it but can’t make it work. Could someone help me with it? Thank you 🙂
-
January 29, 2015 at 10:32 am #3238
JasonKeymasterI’ve had issues with this as well. The validation and sanitization systems are powerful, but I’ve had issues with them always passing.
-
January 29, 2015 at 11:56 am #3239
angeliqueMemberDid you ever manage to make it work? Any clue about what could cause them to always pass?
-
January 29, 2015 at 1:52 pm #3240
angeliqueMemberOk, I’ve been investigating a bit. Here is what I got so far:
First, I had made a mistake in my custom validation declaration (my validation rule was called “html”. I called it “valid_float”. I fixed this, but it didn’t make it work.
What did was to increase the priority parameter when calling the “add_filter” function. The documentation usually show “11”, but it only worked when I put it to 12. I don’t know why.add_filter('piklist_validation_rules', 'check_valid_float', 12); function check_valid_float() { $validation_rules = array( 'valid_float' => array( 'rule' => "/^[1-9]+(.\d+)?(.\d+)?/" ,'message' => __('is not a valid number.') ) ); return $validation_rules; }Now, the numeric validation works on custom post forms BUT:
– it doesn’t work at all on setting forms.
– it doesn’t work anymore if I declare another validation rule (with another name and function name, of course).Any help is welcome, really. Or a workaround for validating piklist forms another way.
-
January 29, 2015 at 4:58 pm #3243
SteveKeymaster@angelique– I just tested on Post Meta and Settings and it worked for me. If you are still having issues, please zip up your plugin and email to [email protected] We’ll be happy to take a look.
As for the Email validation, please verify that it works in the Demos.
-
January 30, 2015 at 3:46 pm #3248
angeliqueMemberHello Steve,
Thank you for the tests you made.
I’ve been investigating a bit further after you gave me the idea of testing with the demos.
I’ve been able to narrow down the issue.
Actually, the email validation breaks (along with some other validation rules but not all of them) as soon as a custom validation is added.
– it breaks when I add myself a new rule
– it breaks when I add Jason’s PiklistHelper.php class, which add validation rules too. (https://github.com/JasonTheAdams/PiklistHelper/blob/master/PiklistHelper.php)So now I wonder if there is something wrong in my plugin “base” file or if it’s a bug?
You’ll find my basic test plugin attached.
To reproduce the bug:
– Make a fresh install of WordPress
– Add and activate the attached plugin (it will ask to add Piklist)
– Install the piklist demo
– Piklist Demo (custom type) > Add New > Validation
– Try to enter an invalid string in “email” and submit. You won’t get an error message for the email (you will for the textfield and some checkbox). Note that you should have more error messages actually (more things shouldn’t validate).Hope you can help me with this!
-
January 30, 2015 at 3:48 pm #3249
-
January 31, 2015 at 12:27 am #3251
SteveKeymasterThe documentation was incorrect. Your code should look like this:
add_filter('piklist_validation_rules', 'check_valid_numeric', 11); function check_valid_numeric($validation_rules) { $validation_rules['valid_amount'] = array( 'rule' => "/^[1-9]+(.\d+)?(.\d+)?/" ,'message' => __('is not a valid number.') ); return $validation_rules; }Documentation has been updated. Sorry for the inconvenience.
Let us know if you’re still having issues.
-
February 2, 2015 at 7:16 pm #3259
angeliqueMemberThank you for your answer.
No luck though. This change does allow the e-mail validation to work (as long as I don’t put back PickListHelper) but the new rule itself doesn’t work. I tried with both snippets: the one based on my RegEx above and the one from the updated documentation. None of them works for me.
Here is a new test plugin.
-
February 2, 2015 at 7:18 pm #3260
SteveKeymaster@angelique– If you don’t use PiklistHelper does it work?
-
February 2, 2015 at 7:20 pm #3261
angeliqueMemberNo, it doesn’t. I comment out PiklistHelper by default to test.
-
February 2, 2015 at 7:38 pm #3262
SteveKeymasterYou didn’t use the
validateparameter correctly. It should look like this:'validate' => array( array( 'type' => 'number' ) )Your code only defined ONE array.
-
February 3, 2015 at 12:40 pm #3264
angeliqueMemberIndeed. It works now.
PiklistHelper has also been updated so now everything work perfect.
Thank you for your help. -
February 3, 2015 at 1:03 pm #3265
SteveKeymasterGreat! Closing ticket. Let us know if you need anything else.
-
-
AuthorPosts
- The topic ‘Validation always pass’ is closed to new replies.