Tagged: 

Viewing 13 reply threads
  • Author
    Posts
    • #3237
      angelique
      Member

      Hello,

      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 🙂

    • #3238
      Jason
      Keymaster

      I’ve had issues with this as well. The validation and sanitization systems are powerful, but I’ve had issues with them always passing.

    • #3239
      angelique
      Member

      Did you ever manage to make it work? Any clue about what could cause them to always pass?

    • #3240
      angelique
      Member

      Ok, 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.

    • #3243
      Steve
      Keymaster

      @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.

    • #3248
      angelique
      Member

      Hello 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!

    • #3249
      angelique
      Member

      Oops, can’t attach ZIP or PHP apparently.
      You can download the plugin here.

    • #3251
      Steve
      Keymaster

      @angelique

      The 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.

    • #3259
      angelique
      Member

      Thank 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.

    • #3260
      Steve
      Keymaster

      @angelique– If you don’t use PiklistHelper does it work?

    • #3261
      angelique
      Member

      No, it doesn’t. I comment out PiklistHelper by default to test.

    • #3262
      Steve
      Keymaster

      You didn’t use the validate parameter correctly. It should look like this:

      'validate' => array(
        array(
          'type' => 'number'
        )
      )
      

      Your code only defined ONE array.

      Please refer to this doc >

    • #3264
      angelique
      Member

      Indeed. It works now.

      PiklistHelper has also been updated so now everything work perfect.
      Thank you for your help.

    • #3265
      Steve
      Keymaster

      Great! Closing ticket. Let us know if you need anything else.

Viewing 13 reply threads
  • The topic ‘Validation always pass’ is closed to new replies.