Welcome Guest, Not a member yet? Register   Sign In
External functions in form_validation.php file
#2

(This post was last modified: 05-01-2017, 07:02 AM by neuron.)

U need to extend form_validation library


in application library create MY_Form_validation.php


PHP Code:
class MY_Form_validation extends CI_Form_validation {
    
    public function 
__construct()
    {
        
parent::__construct();
    }
}


public function 
custom_rule($input){
 
 $CI $get_instance();
$CI->load->model('custom_model');
$result $CI->custom_model->validate_input($input);

return 
$result //true if validated else false;



then add in config.php

PHP Code:
$config = array(
'user_input_rules' => (
 
   array(
 
       'field'  => 'usuario',
 
       'label'  => 'Nombre de usuario',
 
       'rules'  => 'trim|required|max_length[12]|custom_rule',
 
       'errors' => array(
 
                       'is_unique' => 'El nombre de usuario ya está en uso.'
 
                   )
 
   ),    array(
        'field'  => 'usuario2',
        'label'  => 'Nombre de usuario2',
        'rules'  => 'trim|required|max_length[12]|custom_rule',
        'errors' => array(
                        'is_unique' => 'El nombre de usuario ya está en uso.'
                    )
    )
 
   
); 

in controller method:

PHP Code:
public function user_input(){
$this->load->library('form_validation'); // codeigniter will automaticly load your MY_Form_validation.php
$result $this->form_validation->run('user_input_rules');


in application/language/yourlang/form_validation.php 

you can set error message text
Reply


Messages In This Thread
RE: External functions in form_validation.php file - by neuron - 05-01-2017, 07:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB