[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!