【问题标题】:Custom form validation class自定义表单验证类
【发布时间】:2015-05-13 12:59:27
【问题描述】:

我创建了从 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 不能从验证类中捕获验证器?我不想在控制器中使用验证器方法。

有什么想法吗?

【问题讨论】:

    标签: php codeigniter validation


    【解决方案1】:

    我认为问题在于你没有创建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.'); 
    

    【讨论】:

      【解决方案2】:
      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. 
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多