【问题标题】:Extended Form_validation library in codeigniter, cannot check is field is emptycodeigniter 中扩展的表单验证库,无法检查字段是否为空
【发布时间】:2021-06-18 07:12:42
【问题描述】:

我试图扩展 codeigniter(3.1.11) form_validation 库以添加我自己的验证规则。 下面是application/libraries/MY_Form_validation.php写的代码。

defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation
{
    protected $CI;

    public function __construct($rules = array())
    {
        parent::__construct($rules);
        // Assign the CodeIgniter super-object
        $this->CI =& get_instance();
    }

    public function snExists($sn){

        if (empty($sn)) {

            $this->CI->form_validation->set_message('snExists', '{field} is required');
            return FALSE;
        }else{

            $query = $this->CI->db->query("SELECT `sn` FROM `employee` WHERE `sn` = '$sn';");
            $numrows = $query->num_rows();

            if ($numrows > 0) {
                return TRUE;
            }else{
                $this->CI->form_validation->set_message('snExists', '{field} does not exist');
                return FALSE;
            }
        }
    }
}

我面临的问题是,当我提交空字段时,验证不会返回 FALSE。 不知何故,if(empty($sn)) 不满意,else 被执行。 希望有人能帮忙。谢谢。

【问题讨论】:

    标签: validation codeigniter-3


    【解决方案1】:

    因此,如果字段提交为空,codeigniter 似乎不会调用任何验证方法。默认情况下它只会返回TRUE。所以在我的情况下,检查$sn 是否为空是没有意义的。

    【讨论】:

      猜你喜欢
      • 2012-02-27
      • 2013-02-21
      • 2012-06-21
      • 2012-03-01
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      相关资源
      最近更新 更多