Welcome Guest, Not a member yet? Register   Sign In
Form validation and native PHP functions
#1

(This post was last modified: 06-20-2017, 05:24 AM by cvetan.)

I don't understand how the native PHP functions are to be used in form validation.

For instance, i've set this rule 'required|trim|strtolower'.

For test string:

Code:
'   Test'


it doesn't show error, validation passes.

If i missunderstood, what should native functions do in form validation?
"Well, if crime fighters fight crime and fire fighters fight fire, what do freedom fighters fight?"
Reply
#2

(This post was last modified: 06-20-2017, 05:34 AM by Martin7483. Edit Reason: typo )

The only rule that could fail on validation is the required rule. Because something has been provided it will pass as valid.

trim and strtolower are string prepping functions. All you are doing is telling CI to run the native PHP functions to make sure all white space is removed and that all characters are lowercase.

If you want the validation to fail based on these rules you should create your own validation rule to do so.
Reply
#3

(This post was last modified: 06-20-2017, 06:23 AM by cvetan.)

One other thing.

I've set 'required|regex_match[/pattern/]'.

And it didn't show required error, went straight to regex match on empty input?
"Well, if crime fighters fight crime and fire fighters fight fire, what do freedom fighters fight?"
Reply
#4

I believe using native PHP methods is like using a custom callback method. They are always called first. Then followed by all other set rules

I had a solution for it but for reasons unknown that thread was removed. I do still have the code and will look it up for you
Reply
#5

If you always want required rules to run first you can do the following

Create a MY_Form_validation class in ./application/libraries/
Add this code to it

PHP Code:
<?php  

defined
('BASEPATH') OR exit('No direct script access allowed');

class 
MY_Form_validation extends CI_Form_validation {

 
   protected function _prepare_rules($rules)
 
   {
 
       $new_rules = array();
 
       $required_rules = array();
 
       $callbacks = array();

 
       foreach ($rules as &$rule)
 
       {
 
           // Let 'required' always be the first (non-callback) rule
 
           if ($rule === 'required')
 
          {
 
              array_unshift($required_rules'required');
 
           }
 
           // 'isset' is a kind of a weird alias for 'required' ...
 
           elseif ($rule === 'isset' && (empty($new_rules) OR $new_rules[0] !== 'required'))
 
           {
 
              array_unshift($required_rules'isset');
 
           }
 
           // The old/classic 'callback_'-prefixed rules
 
           elseif (is_string($rule) && strncmp('callback_'$rule9) === 0)
 
           {
 
               $callbacks[] = $rule;
 
           }
 
           // Proper callables
 
           elseif (is_callable($rule))
 
           {
 
           $callbacks[] = $rule;
 
           }
 
           // "Named" callables; i.e. array('name' => $callable)
 
           elseif (is_array($rule) && isset($rule[0], $rule[1]) && is_callable($rule[1]))
 
           {
 
                $callbacks[] = $rule;
 
           }
 
           // Everything else goes at the end of the queue
 
           else
            
{
 
           $new_rules[] = $rule;
 
       }
 
   }

 
   // Merge required rules with the callbacks, setting the required rules as the first rules to be run
 
   $callbacks array_merge($required_rules$callbacks);
 
   return array_merge($callbacks$new_rules);
 
   }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB