2015-05-13 78 views
0

我創建了從CI_Form_validation類擴展的My_Form_validation類。自定義表單驗證類

我創建了一個自定義方法,用空格驗證alpha字符串並正常工作。但是,現在我需要驗證數據庫中是否存在一個id。

所以我創建了My_Form_validation的方法來檢查,但無法正常工作,驗證總是返回false:

My_Form_validation

class My_Form_validation extends CI_Form_validation { 

    public function get_error_array(){ 
     return $this->_error_array; 
    } 

    public function alpha_space($value){ 
     $str = str_replace(" ", "", $value); 
     return ctype_alpha($str); 
    } 

    public function entity_exist($id){ 
     return true; 
    } 
} 

驗證規則

'entity_report' => array(
    array(
     'field' => 'id', 
     'label' => 'id', 
     'rules' => 'trim|required|is_natural_no_zero|entity_exist' 
    ) 
) 

爲什麼Codeigniter不能從驗證類中捕獲驗證器是嗎?我不想在控制器中使用驗證器方法。

任何想法?

回答

0

我覺得跟這個問題你不要做的CI

function __construct() { 
parent::__construct(); 
// reference to the CodeIgniter super object 
$this->CI =& get_instance(); 
} 

實例,並設置你的驗證規則類似

$this->CI->form_validation->set_message('email_check', 'The %s is not valid.'); 
0
You can Use like this 

    create form_validation.php file in config folder. 


    $config = array(
    'entity_report' => array(
     array(
      'field' => 'id', 
      'label' => 'ID', 
      'rules' => 'required|min_length[2]' 
     ) 
    ) 
); 

    insert this line in autoload.php 
    $autoload['config'] = array('form_validation'); 

    if ($this->form_validation->run('entity_report') == FALSE) { 
     Your code here. 
    } else { 
    your code here. 
    }