Welcome Guest, Not a member yet? Register   Sign In
Adding new validation
#1

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
Reply
#2

(This post was last modified: 10-12-2021, 12:51 AM by Knutsford.)

(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
Reply
#3

(This post was last modified: 10-12-2021, 01:20 AM by InsiteFX.)

Upgrade to 4, CodeIgniter 2 has security issues.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

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
Reply
#5

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.o...itwise.php
https://www.php.net/manual/en/language.o...ogical.php
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#6

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
Reply
#7

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.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#8

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.
Reply
#9

Believe me, going from 3 to 4 is way more work than 2 to 3.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB