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

[eluser]Unknown[/eluser]
Hi there,
I am new to this lovely framework and I am trying to create a validator class for every controller for the seek of easy maintenance. For example, I have a category controller so I have a validator class as shown below for this conroller

Code:
class Category_validator extends Validator {
    
    /**
     * Class Constructor
     */
    public function __construct() {
        parent::__construct ();
    }
    
    /**
     * Set the category validation rules
     */
    public function set_rules($mode) {
        
        $this->CI->form_validation->set_rules ( 'name', 'Category Name', 'trim|required|min_length[5]|max_length[70]|xss_clean' );
        $this->CI->form_validation->set_rules ( 'active', 'Category Active', 'trim|required|callback_validate_active|xss_clean' );
    }
    
    /**
     * Set error messages
     */
    public function set_messages() {
        # Set any custome form validation error messages over here
    }
    
    /**
     * Runs the specified validation rules
     * @param    none
     * @return    none
     */
    public function run($mode) {
        return parent::run ( $mode );
    }
    
    /**
     * Validate the value of category status
     *
     * @param     string         $value
     * @return     boolean
     */
    public function validate_active($value) {
        
        echo ' inside validation';
        
        if (in_array ( $value, array ('Y', 'N' ) )) {
            return true;
        } else {
            $this->CI->form_validation->set_message ( 'validate_active', 'The %s field can not have the value ' . $value . '' );
            return false;
        }
    }
}

It is working fine but the call back function defined in the second validation rules never gets invoked. Am I doing something wrong? When I define the validate_Active method in the category controller. it works. Does this mean that call back function has to be in the controller and can not be defined somewhere else?


your usual support is highly appreciated
#2

[eluser]WanWizard[/eluser]
Correct.

The form validation library expects callbacks the be in the current controller.
In your example of callback_validate_active, it checks if $this->validate_active() exists before calling it. If not, the rule will be ignored.
#3

[eluser]Unknown[/eluser]
Thanks WanWizard for your answer




Theme © iAndrew 2016 - Forum software by © MyBB