Reputation: 61
My form validation code in controller is,
$this->form_validation->set_rules("name", "Name", "trim|callback_custom_validation");
if ($this->form_validation->run() !== FALSE) {}
My helper code is,
function custom_validation($str) {
// validation logic
}
If i move this custom_validation
function to controller then it is working but it is not working as helper.
I need to call this function from controller and model so i am using helper.
So how to call from helper?
Upvotes: 0
Views: 914
Reputation: 51
Add your custom validation method to any model and use it in controllers and models like this example:
$this->form_validation->set_rules(
'name', 'Name',
array(
'trim',
'required',
array($this->Mymodel_m, 'custom_validation')
)
);
if ($this->form_validation->run() !== FALSE) {...}
More info Callable: Use anything as a rule
Upvotes: 0
Reputation: 687
There are two ways to do this.
You should create a helper file in the application/helpers folder and place the function in the helper file. Then load the helper file in your controller.
custom_validation.php:
function custom_validation($str) {
// validation logic
}
Inside controller:
$this->load->helper('custom_validation');
You can also extend the Form_validation library.
Create a file: application/libraries/MY_Form_validation.php
<?php
class MY_Form_validation extends CI_Form_validation {
public function custom_validation($str) {
// validation logic
}
}
And then just load it as normal validation:
$this->form_validation->set_rules('name', 'Name', 'required|custom_validation');
Upvotes: 1