Custom Validation Function |
Hi guys,
Ive created a function(MyValidation) to validate the input format. MyValidation function is I'm the same controller as my validation. I am able to run the validation by "callback_MyFunction". withOut any issue. Now I'm trying to bring it out of my controller(Ive created a new class in library) that i can use this for another controllers without retype in new controller. My validation to call MyValidation from library PHP Code: $this->form_validation->set_rules('input', 'lang:', 'trim|required|callback_'.$this->functions->MyValidation($this->input->post('input')) ); PHP Code: $this->form_validation->set_rules('input', 'lang:', 'trim|required|callback_'.$this->functions->MyValidation ); PHP Code: $this->form_validation->set_rules('input', 'lang:', 'trim|required|callback_'.$this->functions->MyValidation() ); Any idea?
The easiest thing to do is to create that function in a helper, not a library. As long as the helper is loaded, it will be able to find the function just like it can find any PHP command.
A slightly more "proper" way to do it would be to extend the Form_validation library in a MY_Form_validation file. Add the functions to that, and then you should be able to use it like: Code: $this->form_validation->set_rules('input', 'lang:', 'trim|required|my_validation' ); (09-22-2015, 06:52 AM)kilishan Wrote: The easiest thing to do is to create that function in a helper, not a library. As long as the helper is loaded, it will be able to find the function just like it can find any PHP command. "Kilishan" Thank you for reply. I prefer to user as helper. will test and get back here to share my results.
Ok now I'm trying to have my function in a helper inside helpers/ folder.
But I'm getting this error Code: An Error Was Encountered and my helper written like this PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Ive tried to load my loader like PHP Code: $this->load->helper('validation'); PHP Code: $this->load->helper('validation_helper'); My helper file started with small letter and I've tried to make it upper case (only the 'V') but still is the same. Any Idea? Thanks everybody
Where in your controller are you loading the helper? If you are loading it in a _constructor you must call parent::__constructor like so:
PHP Code: public function __constructor() { (09-30-2015, 02:37 AM)Martin7483 Wrote: Where in your controller are you loading the helper? If you are loading it in a _constructor you must call parent::__constructor like so: Mine is also the same. I written in my construct function. but i can see you written here constructor() instead of construct() ! is it true? i never seen contractor in CI. make me correct if I'm wrong. Thanks for reply
(09-30-2015, 03:13 AM)ardavan Wrote:(09-30-2015, 02:37 AM)Martin7483 Wrote: Where in your controller are you loading the helper? If you are loading it in a _constructor you must call parent::__constructor like so: Sorry! That was a typo... It must be: PHP Code: public function __construct() { The darn things are called constructors. But thats not what you place in your classes ![]() Anyway, you say you are doing things this way in your controller?
(09-22-2015, 06:52 AM)kilishan Wrote: A slightly more "proper" way to do it would be to extend the Form_validation library in a MY_Form_validation file. Add the functions to that, and then you should be able to use it like: I would advise you to use this approach. First of all, it keeps all your code related to form validation together. Second, you can easily override the default rule methods with your own. Here is an example of a MY_Form_validation.php: PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Welcome Guest, Not a member yet? Register Sign In |