CodeIgniter Forums
Adding new validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Adding new validation (/showthread.php?tid=80280)



Adding new validation - Knutsford - 10-11-2021

How do I add a new form validation?


I have
PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class MY_Form_validation extends CI_Form_validation {
      function __construct($config = array()){
   parent::__construct($config);
   $this->CI =& get_instance();
          $this->_config_rules = $config;
    }  
 
    function alpha_space($str) {    
 
        $this->CI->form_validation->set_message('alpha_space', 'The %s may only contain Alpha Characters and Spaces.');
        if (trim($value) !== "" & Is_a_Letter_or_Space($value)) { 
                return TRUE;
        } 
 else {
              return FALSE;
    }
    }
 
 function Is_a_Letter_or_Space($inputbox) {  

 $teststring = Trim($inputbox);
 if ($teststring == "") {
 return False; 

 } 

 $i = 0;
 while ($i <= strlen($teststring) - 1) {
 $c = strtolower(substr($teststring,$i,1)); 

 if ($c <> "a" & $c <> "b" & $c <> "c" & $c <> "d" & $c <> "e" & $c <> "f" & $c <> "g" & $c <> "h" & $c <> "i" & $c <> "j" & $c <> "k" & $c <> "l" & $c <> "m" & $c <> "n" & $c <> "o" & $c <> "p" & $c <> "q" & $c <> "r" & $c <> "s" & $c <> "t" & $c <> "u" & $c <> "v" & $c <> "w" & $c <> "x" & $c <> "y" & $c <> "z" & $c <> " ") { 
 return False;
 }
 $i = $i + 1;
 } 
 return True;
 }


Code:
But it is causing a Javascript error

There are the rules

'order-info-form' => array(
Code:
                                    array(
Code:
                                            'field' => 'order_first_name',
Code:
                                            'label' => 'First name',
Code:
                                            'rules' => 'trim|required|alpha_space'
Code:
                                        ),
Code:
                                    array(
Code:
                                            'field' => 'order_last_name',
Code:
                                            'label' => 'Last name',
Code:
                                            'rules' => 'trim|required|alpha_space'
Code:
                                        ),

etc


Thanks



RE: Adding new validation - Knutsford - 10-12-2021

(10-11-2021, 11:05 AM)Knutsford Wrote: How do I add a new form validation?


I have
PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class MY_Form_validation extends CI_Form_validation {
      function __construct($config = array()){
   parent::__construct($config);
   $this->CI =& get_instance();
          $this->_config_rules = $config;
    }  
 
    function alpha_space($str) {    
 
        $this->CI->form_validation->set_message('alpha_space', 'The %s may only contain Alpha Characters and Spaces.');
        if (trim($value) !== "" & Is_a_Letter_or_Space($value)) { 
                return TRUE;
        } 
 else {
              return FALSE;
    }
    }
 
 function Is_a_Letter_or_Space($inputbox) {  

 $teststring = Trim($inputbox);
 if ($teststring == "") {
 return False; 

 } 

 $i = 0;
 while ($i <= strlen($teststring) - 1) {
 $c = strtolower(substr($teststring,$i,1)); 

 if ($c <> "a" & $c <> "b" & $c <> "c" & $c <> "d" & $c <> "e" & $c <> "f" & $c <> "g" & $c <> "h" & $c <> "i" & $c <> "j" & $c <> "k" & $c <> "l" & $c <> "m" & $c <> "n" & $c <> "o" & $c <> "p" & $c <> "q" & $c <> "r" & $c <> "s" & $c <> "t" & $c <> "u" & $c <> "v" & $c <> "w" & $c <> "x" & $c <> "y" & $c <> "z" & $c <> " ") { 
 return False;
 }
 $i = $i + 1;
 } 
 return True;
 }


Code:
But it is causing a Javascript error

There are the rules

'order-info-form' => array(
Code:
                                    array(
Code:
                                            'field' => 'order_first_name',
Code:
                                            'label' => 'First name',
Code:
                                            'rules' => 'trim|required|alpha_space'
Code:
                                        ),
Code:
                                    array(
Code:
                                            'field' => 'order_last_name',
Code:
                                            'label' => 'Last name',
Code:
                                            'rules' => 'trim|required|alpha_space'
Code:
                                        ),

etc


Thanks




It is Codeigniter 2  if that helps


RE: Adding new validation - InsiteFX - 10-12-2021

Upgrade to 4, CodeIgniter 2 has security issues.


RE: Adding new validation - Knutsford - 10-12-2021

Upgrading to 4 is on the cards sometime What I posted is what I tried and I was getting a JavaScript error - something to do with the order id which didn't make sense at all as it isn't part of the form. I am wondering if I should try callback instead


RE: Adding new validation - includebeer - 10-12-2021

Maybe this has nothing to do with your problem, but “&” is a bitwise operator. You should use “&&” instead.

https://www.php.net/manual/en/language.operators.bitwise.php
https://www.php.net/manual/en/language.operators.logical.php


RE: Adding new validation - Knutsford - 10-13-2021

Yep you are right I have always had a problem with & and && for some reason. I tried doing the validation using callback last night but couldn't get it to work


RE: Adding new validation - includebeer - 10-13-2021

I you don’t want to upgrade to CI4 at the moment, you should at least upgrade to CI3. The steps from CI2 to CI3 are quick and easy and you would at least have documentation for that version. Because CI2 is very outdated and the user guide is no longer available. So it’s hard to tell what’s wrong with your code.


RE: Adding new validation - Knutsford - 10-14-2021

Hmm some version upgrades are easy peasy but I don't remember going from 2 to 3 that easy when I have done it depending on how the site it has been written. Anyway I have got it working by being naughty and changing the core code which is what I aas trying to avoid.


RE: Adding new validation - includebeer - 10-14-2021

Believe me, going from 3 to 4 is way more work than 2 to 3.