CodeIgniter Forums
Form validation and native PHP functions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Form validation and native PHP functions (/showthread.php?tid=68288)



Form validation and native PHP functions - cvetan - 06-20-2017

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?


RE: Form validation and native PHP functions - Martin7483 - 06-20-2017

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.


RE: Form validation and native PHP functions - cvetan - 06-20-2017

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?


RE: Form validation and native PHP functions - Martin7483 - 06-20-2017

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


RE: Form validation and native PHP functions - Martin7483 - 06-20-2017

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);
 
   }