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

(This post was last modified: 04-28-2020, 09:41 AM by jreklund.)

I am trying to add in validation for just Alphabetic and spaces but something seems to be wrong with this. It is in MY_Form_validation.php
under /libraries. Can anyone see anything wrong with? It is version 3 of codeigniter

PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class MY_Form_validation extends CI_Form_validation {
    protected $CI;

      public function __construct() {
          
        $this->CI =& get_instance();

        // Validation rules can be stored in a config file.
        $this->_config_rules = $rules;

        // Automatically load the form helper
        $this->CI->load->helper('form');

        // Set the character encoding in MB.
        if (function_exists('mb_internal_encoding'))
        {
            mb_internal_encoding($this->CI->config->item('charset'));
        }

        log_message('debug', "Form Validation Class Initialized");
      }
    
    function alpha_space($str) {     
        
        $this->CI->form_validation->set_message('alpha_space', 'The %s may only contain Alpha Characters and Spaces.');
        if (trim($str) !== "" & Is_a_Letter_or_Space_validation($str)) {             
                return TRUE;
         } 
        else {
              return FALSE;
        }
    }
    
    function Is_a_Letter_or_Space_validation($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;
    }



The problem is in

Code:
events: {
        'click .submit-order-info': 'onSubmitClick'
    },

    initialize: function() {

    },

    el: '#order-info-form',

    submit: function (callback) {
        $.ajax({
            url: DT_CONFIG.baseURL + 'ajax/submitorder', dataType: 'json', cache: false, timeout: 10000,
            type: "POST",
            data: this.$el.serialize(),
            success: function (data) {
                if (data.status === "success") {
                    callback(null, data);
                } else {
                    callback(data.errors, data);
                }
            },
            error: function () {
                callback('GENERAL_ERROR');
            }
        });
    },

    onSubmitClick: function () {
        var that = this;
        this.$el.find(".form-errors").slideUp(500);
        this.submit(function(err,data) {
            if(err) {
                var $formErrors = that.$el.find(".form-errors");
                /* Errors found */
                $formErrors.html("<h4>" + that.$el.find(".form-errors h4").text() + "</h4>" + err);
                $formErrors.slideDown(500);
            } else {
                window.location = DT_CONFIG.baseURL + "step/shipping?purchase=" + data.orderData.order_id;
            }
        });

It is going into the else statement in onSubmitClick when there is an error

Thanks. I haven't added a new validater before so it is probably something obvious
Reply
#2

(This post was last modified: 04-28-2020, 09:46 AM by jreklund.)

Does your function work without Ajax? It's seams so complicated.

This rule does the exact save thing.
Code:
'regex_match['/^[a-z\s]+$/']'
https://codeigniter.com/userguide3/libra...-reference
Reply
#3

(This post was last modified: 04-28-2020, 10:54 AM by Knutsford.)

(04-28-2020, 07:12 AM)Knutsford Wrote: I am trying to add in validation for just Alphabetic and spaces but something seems to be wrong with this. It is in MY_Form_validation.php
under /libraries. Can anyone see anything wrong with? It is version 3 of codeigniter

PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class MY_Form_validation extends CI_Form_validation {
    protected $CI;

      public function __construct() {
          
        $this->CI =& get_instance();

        // Validation rules can be stored in a config file.
        $this->_config_rules = $rules;

        // Automatically load the form helper
        $this->CI->load->helper('form');

        // Set the character encoding in MB.
        if (function_exists('mb_internal_encoding'))
        {
            mb_internal_encoding($this->CI->config->item('charset'));
        }

        log_message('debug', "Form Validation Class Initialized");
      }
    
    function alpha_space($str) {     
        
        $this->CI->form_validation->set_message('alpha_space', 'The %s may only contain Alpha Characters and Spaces.');
        if (trim($str) !== "" & Is_a_Letter_or_Space_validation($str)) {             
                return TRUE;
         } 
        else {
              return FALSE;
        }
    }
    
    function Is_a_Letter_or_Space_validation($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;
    }



The problem is in

Code:
    events: {
        'click .submit-order-info': 'onSubmitClick'
    },

    initialize: function() {

    },

    el: '#order-info-form',

    submit: function (callback) {
        $.ajax({
            url: DT_CONFIG.baseURL + 'ajax/submitorder', dataType: 'json', cache: false, timeout: 10000,
            type: "POST",
            data: this.$el.serialize(),
            success: function (data) {
                if (data.status === "success") {
                    callback(null, data);
                } else {
                    callback(data.errors, data);
                }
            },
            error: function () {
                callback('GENERAL_ERROR');
            }
        });
    },

    onSubmitClick: function () {
        var that = this;
        this.$el.find(".form-errors").slideUp(500);
        this.submit(function(err,data) {
            if(err) {
                var $formErrors = that.$el.find(".form-errors");
                /* Errors found */
                $formErrors.html("<h4>" + that.$el.find(".form-errors h4").text() + "</h4>" + err);
                $formErrors.slideDown(500);
            } else {
                window.location = DT_CONFIG.baseURL + "step/shipping?purchase=" + data.orderData.order_id;
            }
        });

It is going into the else statement in onSubmitClick when there is an error

Thanks. I haven't added a new validater before so it is probably something obvious

I have used it several times before. You are right the regex would be better. It doesn't work even if I don't use the alpha_space rule.
Reply
#4

Based on the code provided (if you don't have anything more), you don't execute anything.
Reply
#5

(This post was last modified: 04-28-2020, 11:39 AM by Knutsford.)

Not if I don't a rule alpha_space to something. I am not worried about that for now. I just don't know why with the existing rules when I upload my MY_Form_validation.php I don't get any error message when I do without MY_Form_validation.php. I suspect it is is something to do with the public function __construct() {

Do the other functions need to be Public?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB