Welcome Guest, Not a member yet? Register   Sign In
Problem with validation_errors() and Hooks
#1

[eluser]Israel Pereira[/eluser]
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
Code:
/*
  * 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
Code:
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();
Code:
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:
Code:
...
  
  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:
Code:
{"status":1,"message":""}

Thanks again!
#2

[eluser]Aken[/eluser]
Try:

Code:
// 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.
#3

[eluser]Israel Pereira[/eluser]
[quote author="Aken" date="1352437660"]Try:

Code:
// 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.[/quote]

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!
#4

[eluser]Israel Pereira[/eluser]
Someone else?
#5

[eluser]Aken[/eluser]
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).
#6

[eluser]Israel Pereira[/eluser]
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!




Theme © iAndrew 2016 - Forum software by © MyBB