简体   繁体   English

CodeIgniter自定义验证库功能不起作用

[英]CodeIgniter custom validation library function not working

I've created custom validation library class MY_Form_validation as MY_Form_validation.php in application/libraries as follows. 我已经在application/libraries创建了自定义验证库类MY_Form_validation作为MY_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;
    }
}

?>

In my validation function I've included following rules as follows. 在我的验证功能中,我包括以下规则。

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;

} 

The core validation rules are working fine but custom rule is not working. 核心验证规则运行正常,但自定义规则无效。 So please help me to get the soultion and Its literally wasting my time. 因此,请帮助我获得灵魂,这实际上在浪费我的时间。 The work would be more appreciated. 这项工作将不胜感激。

Check the rules in the function validate() 检查功能validate()中的规则

What I think it's incorrect: 我认为这是不正确的:

$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'
        ),
    );

What I think is correct: 我认为是正确的:

$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'
        ),
    );

I think good is not a php function, or an internal codeigniter function. 我认为好的不是php函数,也不是内部codeigniter函数。

Edit: 编辑:

What about using: 如何使用:

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

Instead of 代替

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

Using === means the value MUST BE integer 0, but if $file['size'] returns 0 as string the if won't be true, and the function always will return true. 使用===表示值必须为整数0,但是如果$ file ['size']以字符串形式返回0,则if不会为true,并且该函数将始终返回true。

As far as i understand your function always return true. 据我了解,您的函数始终返回true。 Because of $file Not $_FILES 因为$ 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;
}

I had the same problem and found the cause while looking in CodeIgniter's source code. 我在查找CodeIgniter的源代码时遇到了同样的问题,并找到了原因。 It seems the writers thought that if a field didn't have "required", then it would just skip all the rules and always return that the form has validated. 似乎作者认为,如果没有“必填”字段,则它将跳过所有规则,并始终返回已验证表单的信息。 See it for yourself from their code: 从他们的代码中亲自查看:

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

However, if you add "callback_" in front of your rule, you can still make it run, for the procedure, look here: https://www.codeigniter.com/userguide2/libraries/form_validation.html#callbacks 但是,如果您在规则前面添加“ callback_”,则仍然可以使其运行,对于该过程,请查看此处: https : //www.codeigniter.com/userguide2/libraries/form_validation.html#callbacks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM