【问题标题】:CodeIgniter custom validation library function not workingCodeIgniter 自定义验证库函数不起作用
【发布时间】:2013-11-25 09:28:15
【问题描述】:

我在application/libraries 中创建了自定义验证库类MY_Form_validationMY_Form_validation.php,如下所示。

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


class MY_Form_validation extends CI_Form_validation {

    public function __construct($rules = array()) {
          parent::__construct($rules);
    }

    public function file_required($file) {


          if($file['size']===0) {
              $this->set_message('file_required', 'Uploading a file for %s is required.');
              return false;
          }

          return true;
    }
}

?>

在我的验证函数中,我包含了以下规则。

public function validate() {

    $this->load->library('form_validation');

    $config = array(
        array(
            'field' => 'name',
            'label' => 'Name',
            'rules' => 'trim|required|xss_clean'
        ),
        array(
            'field' => 'display_photo',
            'label' => 'Display Photo',
            'rules' => 'trim|good|file_required|xss_clean'
        ),
    );

    $this->form_validation->set_rules($config);

    if ($this->form_validation->run()) {
        return true;
    }
    return false;

} 

核心验证规则工作正常,但自定义规则不工作。所以请帮助我得到灵魂,它实际上是在浪费我的时间。这项工作将受到更多赞赏。

【问题讨论】:

    标签: codeigniter function validation


    【解决方案1】:

    据我了解,您的函数始终返回 true。因为 $file 不是 $_FILES

    public function file_required($file) {
        if($_FILES[$file]['size']===0) {
            $this->set_message('file_required', 'Uploading a file for %s is required.');
            return false;
        }
        return true;
    }
    

    【讨论】:

    • 它还没有工作..即使回显某些内容也没有显示任何内容。
    【解决方案2】:

    检查函数validate()中的规则

    我认为这是不正确的:

    $config = array(
            array(
                'field' => 'name',
                'label' => 'Name',
                'rules' => 'trim|required|xss_clean'
            ),
            array(
                'field' => 'display_photo',
                'label' => 'Display Photo',
                'rules' => 'trim|good|file_required|xss_clean'
            ),
        );
    

    我认为是正确的:

    $config = array(
            array(
                'field' => 'name',
                'label' => 'Name',
                'rules' => 'trim|required|xss_clean'
            ),
            array(
                'field' => 'display_photo',
                'label' => 'Display Photo',
                'rules' => 'trim|file_required|xss_clean'
            ),
        );
    

    我认为好的不是php函数,也不是内部codeigniter函数。

    编辑:

    如何使用:

    if ($file['size'] == 0) {
    

    代替

    if ($file['size'] === 0) {
    

    使用 === 表示值必须是整数 0,但如果 $file['size'] 以字符串形式返回 0,则 if 将不为真,函数始终返回真。

    【讨论】:

    • 从apache检查error_log,或者在codeigniter中激活error_logging,看看代码是否有错误。
    【解决方案3】:

    我在查看 CodeIgniter 的源代码时遇到了同样的问题并找到了原因。似乎作者认为,如果一个字段没有“必需”,那么它将跳过所有规则并始终返回表单已验证。从他们的代码中亲自查看:

    // If the field is blank, but NOT required, no further tests are necessary
    if ( ! in_array('required', $rules) AND is_null($postdata))
    

    但是,如果您在规则前添加“callback_”,您仍然可以使其运行,有关过程,请看这里: https://www.codeigniter.com/userguide2/libraries/form_validation.html#callbacks

    【讨论】:

      猜你喜欢
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 2015-05-16
      • 2012-05-02
      • 2017-11-28
      • 2017-03-14
      • 1970-01-01
      相关资源
      最近更新 更多