application / config / form_validation.php
$config = array(
'search' => array(
array('field'=>'', 'label'=>'', 'rules'=>''),
array('field'=>'', 'label'=>'', 'rules'=>'')
),
);
-
コントローラ
public function search(){
//is ajax request?
if( !$this->input->is_ajax_request() )
{
return show_error('Bad Request!'); // bye bye, stop execution
}
$this->output->set_status_header('200');
if( ! $this->form_validation->run('search') )
{
echo json_encode(array(
'error' => 1,
'errors' => array(
'field' => form_error('field')
)
));
return; // bye bye, stop execution
}
//all good, continue executing
//.....
}
編集
//For just a constructor
protected $isValidated = TRUE;
public function __construct(){
//check for valid ajax request
//do this in parent class(ajax_controller) ?
if( !$this->form_validation->run('search') )
{
$this->isValidated = FALSE;
return echo json_encode( array('error' => 1));
}
if($isValidated)
{
$this->_search( $this->input->post() );
}
}
protected function _search( $input ){}
-
class Ajax_Controller extends CI_Controller{
public function __construct(){
if( !$this->input->is_ajax_request() )
{
return show_error('Bad Request!');
}
$this->output->set_status_header('200');
}
}