I have been stuck for a while trying to setup my validation rules, on my controller I have this:
public function login () {
$this->load->library('form_validation');
$this->form_validation->set_rules(array(
'field'=>'username',
'label'=>'Username',
'rules'=>'required'
));
if (!$this->form_validation->run()) {
$this->json(array(
'status'=>'error',
'errors'=>$this->form_validation->error_array(),
'test'=>$this->form_validation->get_options()
));
return;
}
$user = $this->MUser->findByAttributes(array('username'=>$this->input->post('username')));
if(!$user) {
$this->json(array(
'status'=>'error',
'errors'=>array('username'=>'Username is not yet registered.')
));
return;
}
$encpass = sha1($user->salt.$this->input->post('password'));
if ($encpass != $user->password) {
$this->json(array(
'status'=>'error',
'errors'=>array('password'=>'Invalid username/password, please try again.')
));
return;
}
$this->json(array(
'status'=>'success',
'errors'=>''
));
}
I also have an extended base controller class and an extended form validation class which are as follows:
class AM_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function render_template($view, $data = array(), $internal = false) {
$view_contents = $this->load->view($view, $data, true);
$header = $internal?$this->load->view('internal/header',$data,true):$this->load->view('external/header',$data,true);
$footer = $internal?$this->load->view('internal/footer',$data,true):$this->load->view('external/footer',$data,true);
$this->output->set_status_header(200,'ok');
$this->load->view('general/template',array_merge(array(
'header'=>$header,
'footer'=>$footer,
'content'=>$view_contents
),$data));
}
public function json($data) {
$this->output->set_status_header(200,'ok')
->set_content_type('application/json')
->set_output(json_encode($data));
}
}
class AM_Form_validation extends CI_Form_validation {
public function __construct($options = array()) {
parent::__construct($options);
}
public function correct_captcha ($str) {
$this->set_message('correct_captcha','Invalid %s answer.');
return $this->CI->session->userdata('captcha_answer') == $str;
}
public function get_options () {
return $this->_field_data;
}
}
But the problem is every time I try to test the form validation, it always returns false regardless if I have entered some value or not. Any idea what is causing this bug?
By the way, I also tried using CI_Controller only for my controller class, but still no luck
Problem solved, upon hours of testing seems like my POST data was not reaching my server. The fail safe of my htaccess made it look like the rewrite was actually working but it wasn't, it was like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /appmon/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /appmon/index.php
</IfModule>
After trying things without htaccess things worked, properly. Thanks to everyone