【问题标题】:Extending codeigniter form_validation class returns undefined扩展 codeigniter form_validation 类返回 undefined
【发布时间】:2016-03-10 07:53:51
【问题描述】:

我做了一个简单的类扩展CI_Form_validation class 并将它放在application\libraries\MY_Form_validation.php 中。这是文件中的代码:

<?php

class MY_Form_validation extends CI_Form_validation {


    public function __construct(){

        parent::__construct();

    }

    public function uniquekey($str){

       if($str == 'anda'){
          $this->form_validation->set_message('uniquekey', "The category name has been used");
          return false;
       } else {
          return true;
       }
    }
}

?>

在另一个名为Category 的类中,我做了$this-&gt;load-&gt;library('form_validation'); 并在验证输入文本框时使用了上面的回调函数。

但是,当我尝试提交表单时,服务器返回两个错误框,第一个错误说:

Message: Undefined property: MY_Form_validation::$form_validation
Filename: libraries/MY_Form_validation.php

第二个错误说:

Message: Call to a member function set_message() on null
Filename: /opt/lampp/htdocs/onesource_reboot/application/libraries/MY_Form_validation.php

我的代码做错了什么?我从here 阅读并使用codeigniter 3.0.4 版


感谢narfdevpro,我终于成功了,当我插入单词“anda”并提交表单时,表单验证将返回false 并显示所需的错误消息。 我尝试了一些涉及数据库交互的更复杂的验证。我在构造函数中添加了这一行,仍然在同一个类中:

public function __construct(){

    parent::__construct();
    $this->CI->load->database();

}

并将我的 uniquekey 函数更改为:

public function uniquekey($str){

    $this->CI->db->select('CategoryName');
    $this->CI->db->where('CategoryName', $str);
    $result = $this->CI->db->get('category');
    if($result->num_rows > 0){
        $this->set_message('uniquekey', 'Kategori telah terpakai');
        return false;
    } else {
        return true;
    }

}

并提交了表单,这是我得到的结果:

Unable to access an error message corresponding to your field name Kategori.(uniquekey)

现在呢?


解决了。 我不知道这是否是一个错误,但这是我为使其正常工作所做的工作。

public function uniquekey($str){

    $this->CI->db->select('CategoryName');
    $this->CI->db->where('CategoryName', $str);
    $result = $this->CI->db->get('category');
    $numrows = $result->num_rows();

    if(numrows > 0) {
        $this->set_message('uniquekey', 'Kategori telah terpakai');
        return false;
    } else {
        return true;
    }

}

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您不是在检查条件,而是在此处分配值:

    if($str = 'anda'){
    

    应该是:

    if($str == 'anda'){
    

    = 符号用于赋值
    == 符号用于比较值

    【讨论】:

    • 这确实解决了一个有效的问题,但并没有真正回答问题。
    • @AnggieAriWidhia:选择最佳答案并标记为已接受。meta.stackexchange.com/questions/5234/…
    • 还有一个问题,你能再帮我解决一下吗?
    • @AnggieAriWidhia:你在哪里使用这个回调函数 uniquekey() ?此值与类别不匹配
    • 我在MY_Form_validation 中使用了这个回调函数。现在解决了
    【解决方案2】:

    您只能从内部控制器和/或模型中使用$this-&gt;form_validation

    当您在库本身中时,您处于不同的范围内,form_validation 属性不存在...而且您一开始就不需要它 - 您只需使用 $this ,所以它是$this-&gt;set_message() 而不是$this-&gt;form_validation-&gt;set_message()

    了解 OOP 的工作原理:http://www.php.net/manual/en/language.oop5.basic.php

    【讨论】:

    • 我以为是 parent::set_message() 因为我正在扩展父母的功能。感谢您的回答。
    • 还有一个问题,你能再帮我解决一下吗?
    • 你不应该在原来的问题上添加越来越多的问题......而是打开一个新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多