2016-03-13 30 views
0

我已經構建了一個簡單的表單驗證,我打算將它用於ajax調用,但現在我似乎無法獲取我的錯誤消息?我知道表單驗證程序正在運行,並且仍然可以看到我在ci_form_validation->_error_array中想要的錯誤,但這是一個受保護的屬性:((這是當我打印該類時)codeigniter validation_errors()empty yet ci_form_validation - > _ error_array顯示我的錯誤?

表單驗證程序工作正常!我已經使用過這種方法數千次之前,但我似乎無法得到我的錯誤消息這一次,我不知道爲什麼?我會保持下面的代碼簡單,爲測試我已經幾乎刪除了所有東西

Ajax控制器(份移除)

<?if(!defined('BASEPATH'))die(); 
class Ajax extends MY_Controller{ 
    public function __construct(){ 
     //if(!isset($_POST)&&!empty($_POST))redirect('/','refresh'); 
     parent::__construct(); 
     //check this site with requested uri or request server url 
     $this->con['validation']['add_user']=array(
      array('field'=>'firstname','label'=>'First name','rules'=>'max_length[30]|required|alpha'), 
      array('field'=>'lastname','label'=>'Last name','rules'=>'max_length[30]|required|alpha'), 
      array('field'=>'email','label'=>'Email address','rules'=>'required|is_unique[users.email]|max_length[150]|valid_email')      
     ); 
     //$this->html->set_doc_type('html');//set application json 
     //header('Content-Type: application/json'); 
    } 
    public function add_user(){ 
     if($this->_fv('add_user')){ 
      $this->load->model('users'); 
      $result=$result=$this->users->insert($this->input->post('firstname'),$this->input->post('lastname'),$this->input->post('email')); 
      //var_dump('hi there'); 
      echo $result; 
     }else{ 
      var_dump($this->fv); 
      var_dump($this->input->post()); 
      var_dump('returned false'); 
      var_dump(validation_errors()); 
      $result=array('firstname'=>form_error('firstname'),'lastname'=>form_error('lastname'),'email'=>form_error('email')); 
      echo json_encode($result); 
     } 
     $this->load->view('index/signup'); 
     //exit; 
    } 
} 

我控制器

<?if(!defined('BASEPATH'))die(); 
class MY_Controller extends CI_Controller{ 
    public$con; 
    public function __construct(){ 
     parent::__construct(); 
    } 
    public function _fv($form=null){ 
     if($form==null)return true; 
     $this->load->library('form_validation'); 
     $this->load->library(array('form_validation'=>'fv')); 
     $this->fv->set_rules($this->con['validation'][$form]); 
     if($this->fv->run()===false){ 
      return false; 
     }else{ 
      return true; 
     } 
    } 
} 

就像我說的,奇怪的是,我可以看到在ci_form_validation對象中的錯誤信息,但它只是沒有在form_error()validation_errors()顯示?

我錯過了一個自動加載庫或幫手嗎?這是我的裝載器

$autoload['helper'] = array('url','form'); 
$autoload['libraries'] = array('HTML'=>'html','database','session'); 
//HTML is my header and footer building library. 

我根本沒有收到任何PHP錯誤消息。一切正常,我只是沒有得到任何錯誤信息? :'(

該錯誤消息我想是is_uniqiue

["_error_array":protected]=> array(1) { ["email"]=> string(52) "The Email address field must contain a unique value." } 

回答

0

我找到了解決我的猜測是,有什麼毛病我的工作方式,但我不知道我種的。?。作弊我的解決方案,但如果你有任何想法,讓我知道!:)

<?if(!defined('BASEPATH'))die(); 
class MY_Form_validation extends CI_Form_validation{ 
    public function __construct(){ 
     parent::__construct(); 
    } 
    public function get_errors(){ 
     return$this->_error_array; 
    } 
} 
相關問題