EllisLab text mark
Advanced Search
     
Problem with validation_errors() and Hooks
Posted: 09 November 2012 12:34 AM   [ Ignore ]
Joined: 2012-11-08
5 posts

Hi guys, I’m having some troubles with CI. Always when I use Hooks my validation_errors() doesn’t work properly!
When I use both, actually validation_errors works (validate fields), but messages came blank. If I take out my Hooks file, messages appears normally.

I use Hooks in ‘post_controller_constructor’ to check in every page/class/function(...) if user is or is not logged in the system.

I have not too much experience in CI, so if anyone can help me, I would be very grateful.

Hugs!

The following codes:

config/Hooks.php

/*
  * This is used to see if in restrictive pages the user is or isn't logged in
  * Note that pages passed in parameters aren't restrictive
  */
 
$hook['post_controller_constructor'= array(
  
'class'    => 'user_authentication',
  
'function' => 'index',
  
'filename' => 'user_authentication.php',
  
'filepath' => 'hooks',
  
'params'   => array('start''login')
 ); 

hooks/user_authentication.php

class user_authentication extends CI_Controller {

 
public function __construct(){
  parent
::__construct();
 
}

 
public function index($params){
  
if(!in_array($this->uri->segment(2), $params)){
   
//Verify if user is logged
   
$this->loginsession->verify();
  
}
 }

$this->loginsession->verify();

class LoginSession {
 
 
public $CI;

 public function 
__construct(){
  $this
->CI =& get_instance();
 
}
 
 
public function verify(){
  
if(!$this->isLogged()){
   redirect
("login");
  
}
 }
 
 
public function isLogged(){
  
if(array_key_exists("isLogged"$this->CI->session->all_userdata())){
   
if($this->CI->session->userdata("isLogged")){
    
//return false;
    
return true;
   
}else{
    
//return true;
    
return false;
   
}
  }else{
   
return false;
  
}
 }
 

This is how do I call in controller:

...
  
  if(
$this->input->is_ajax_request()){
   $this
->_validate();
   if(
$this->form_validation->run($this)==FALSE){
    
die(json_encode(array("status"=>1,"message"=>validation_errors())));
   
}
  }
  
  
...

 public function 
_validate(){
  $config 
= array();
  
$config[] = array(
   
'field' => 'txtAirport',
   
'label' => $this->LanguagesText->login_index__login_airport,
   
'rules' => 'trim|max_length[1]|numeric|htmlspecialchars|required|xss_clean|callback_verify_select_airport'
  
);
  
$config[] = array(
   
'field' => 'txtLogin',
   
'label' => $this->LanguagesText->login_index__login_user,
   
'rules' => 'trim|max_length[20]|alpha_numeric|htmlspecialchars|required|xss_clean'
  
);
  
$config[] = array(
   
'field' => 'txtPassword',
   
'label' => $this->LanguagesText->login_index__login_password,
   
'rules' => 'trim|max_length[80]|alpha_numeric|htmlspecialchars|required|xss_clean'
  
);
  
//Form validation stuffs
  
$this->form_validation->set_rules($config);
  
$this->form_validation->set_error_delimiters('|','');
  
  
//Sets new messages
  
$set_message = array('alpha','alpha_dash','alpha_numeric','decimal','exact_length','greater_than','integer','isset','is_natural','is_natural_no_zero','is_numeric','is_unique','less_than','matches','max_length','min_length','numeric','regex_match','required','valid_email','valid_emails','valid_ip','valid_url');
  for(
$i=0;$i<count($set_message);$i++){
   $this
->form_validation->set_message($set_message[$i]$this->Error->get($set_message[$i]));
  
}
  $this
->form_validation->set_message('verify_select_airport'$this->Error->get('personal_airport'));
 
}
  
... 

And this is what I’m getting in json:

{"status":1,"message":""

Thanks again!

 Signature 

LDS Online - Software Development
Israel Pereira
E-mail: .(JavaScript must be enabled to view this email address)
Website: http://www.ldsonline.com.br

 
Posted: 09 November 2012 01:07 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

Try:

// NO
$this->form_validation->run($this)

// YES
$this->form_validation->run() 

BTW, using a base MY_Controller might be easier than a hook for running a simple check to see if a user is logged in or not.

 
Posted: 09 November 2012 01:32 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-11-08
5 posts
Aken - 09 November 2012 01:07 AM

Try:

// NO
$this->form_validation->run($this)

// YES
$this->form_validation->run() 

BTW, using a base MY_Controller might be easier than a hook for running a simple check to see if a user is logged in or not.

Hi Aken, thanks for answering!

Well, I did what you show me $this->form_validation->run() (without $this) but I’m getting the same error.

I use Hooks instead MY_Controller because I need to verify in every Controller if user is or isn’t logged in. If I do this in Controller, every time I need to call $this->loginsession->verify() in every method. Hooks makes it much easier for me, cause I’ve a lot of another controllers and another hundreds methods!

Any more suggestion?

Thanks a lot!

 Signature 

LDS Online - Software Development
Israel Pereira
E-mail: .(JavaScript must be enabled to view this email address)
Website: http://www.ldsonline.com.br

 
Posted: 10 November 2012 04:40 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-11-08
5 posts

Someone else?

 Signature 

LDS Online - Software Development
Israel Pereira
E-mail: .(JavaScript must be enabled to view this email address)
Website: http://www.ldsonline.com.br

 
Posted: 10 November 2012 07:35 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

If you create a MY_Controller, you add the check to the constructor. Then, all of your individual controllers extend MY_Controller, automatically performing the check. Search around for more info on MY_Controller (aka Base controllers).

 
Posted: 11 November 2012 12:59 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2012-11-08
5 posts

Hi Aken!

Well, I did what you tell me to do and it worked!

I created a function in MY_Controller with my validation and called from __construct().

So, thank you so much for your time!

 Signature 

LDS Online - Software Development
Israel Pereira
E-mail: .(JavaScript must be enabled to view this email address)
Website: http://www.ldsonline.com.br