Welcome Guest, Not a member yet? Register   Sign In
storing callback validation functions in helper file?
#1

[eluser]mike34666[/eluser]
hi, i want to store my validation call backs in a helper file so that i can use them in multiple controllers. i tried to do this and the form validating isnt calling the function in the helper.
Code:
// controller

$this->load->helper('validate');
$this->load->library('form_validation');

$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_val_password');

$this->form_validation->run() // always equals true

// validate_helper.php

if( ! function_exists('val_password')) {
    function val_password($string) {
            return false;
        }
}
#2

[eluser]CroNiX[/eluser]
It would be much better to just extend the validation class with your additional rules.

/application/libraries/MY_Form_validation.php
Code:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
    function MY_Form_validation()
    {
        parent::CI_Form_validation();
    }

    function val_password($string) {
        $this->set_message('val_password', 'The password you entered in the %s field is wrong....loser!');
        return false;
    }
    //...add more rules
}
However, when you extend the class, this just becomes another rule available to the form validation class and not a callback rule, so it would be called like:
$this->form_validation->set_rules('password', 'Password', 'trim|required|val_password');
You also don't need to check if the function is defined before defining it this way.
#3

[eluser]mike34666[/eluser]
thanks croNiX, i hadnt thought of that. I will give it a try tonight.




Theme © iAndrew 2016 - Forum software by © MyBB