• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
External functions in form_validation.php file

#1
Hi.

I want use form_validation.php file with my rules that I put in application/config.

My problem is that some rules need use functions that are for example in models.

I have this in my form_validation.php file:

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


The function "db_table('table_name')" is in the model: examples/examples_model

If I don't use form_validation.php file, I put in my controller:
$this->load->model('examples/examples_model');

Then I can use function "db_table()" without problems when I set set_rules:
$this->form_validation->set_rules('usuario', 'Nombre de usuario', 'trim|required|max_length[12]|is_unique[' . db_table('user_table') . '.username]');

My problem is that if I use form_validation.php file instead set all rules in my controller, I can't use function "db_table()" because it's undefined.

How can I use this external function in my form_validation.php file? Is possible?

Thanks.
Reply

#2
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

#3
Hi, neuron.
Thank you very much for your help.

I had MY_Form_validation.php extending form_validation_library yet with several custom validation functions.
My problem was that I needed use a function is in a model (examples_model), for general use, not for validation. In that case I have tried this in form_validation.php file and it works:

PHP Code:
$this->CI =& get_instance();
$this->CI->load->model('examples/examples_model'); 

And then, I could use the function db_table of model Examples_model with:

PHP Code:
$this->CI->examples_model->db_table('table_name'

Maybe is very complex.

Regards.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.