【问题标题】:Unable to access an error message corresponding to your field name无法访问与您的字段名称对应的错误消息
【发布时间】:2012-05-23 16:32:39
【问题描述】:

我有一个 check_captcha 回调函数,它查看 $row==0 还是 == 1(此信息是从 sql 查询的)。

问题是我不能从$self->form_validation->set_rule('captcha', 'call_back_check_captcha') 调用它,因为我的函数接受$row var。我现在调用它的方式是我收到无法访问错误消息。我怎样才能做到这一点?

function check_captcha( $row)
{
    if($row ==0)//didnt find any
    {
        $this->form_validation->set_message('captcha', 'text dont match captcha');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}


function create_member() 
        {   

            $past = time() - 7200; 

        $this->db->query("DELETE FROM captcha WHERE captcha_time <".$past);                 
        $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word =? AND ip_address =?";     
        $binds = array($_POST['captcha'], $this->input->ip_address(), $past);
        $query= $this->db->query($sql, $binds);

        $row = $query->row(); //row query rows : if it found an entry =1 

            $self->check_captcha($row->count);

        //VALIDATIONS
        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');      
        $this->form_validation->set_rules( 'email_address', 'Email Address', 'trim|required|valid_email|unique[user.email_address]');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|unique[user.username]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_leng[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation','trim|required|matches[password]');
        if(!$_POST['captcha']){
        $this->form_validation->set_rules('captcha', 'Captcha','trim|required');}else{
        $this->form_validation->set_rules('captcha', 'Captcha', 'callback_check_captcha');}

        if($this->form_validation->run()==FALSE)
        {   //this -> to the curr obj(UserController) && registraion() points to the the function in controller
            $this->registration();  //reloads reg page so they can fill out right stuff
        }
        else

【问题讨论】:

  • 你应该在函数名前面添加一个_,这样我就不能从url调用。 check_captcha 应该是 _check_captcha

标签: php codeigniter


【解决方案1】:
$this->form_validation->set_message('check_captcha', 'text dont match captcha');

消息名称对应于函数,而不是字段。因此将其设置为“check_captcha”将修复您的错误。错误消息将使用正确的字段名称。

【讨论】:

  • 犯了同样的错误thnx
  • 这让我很困惑,你在这里使用set_message(),但问题开始使用set_rules()。这是你的错字吗?顺便说一句,我也使用set_rules() 并且有相同的消息。
  • 我使用了函数名而不是字段名,但它仍然显示相同的错误消息。我已经有一个包含错误消息的单独语言文件,所以我不想在form_validation_lang 文件以避免将来出现差异。我现在该怎么办?
【解决方案2】:

实际上最好的方法是在语言上添加此条目“check_captcha”,而不是直接在控制器上编写错误消息。

在我的情况下,验证规则(表单验证)“less_than”的消息不存在。

我更改了文件 /system/language/??/form_validation_lang.php。我已经添加了缺失的条目。

【讨论】:

  • 是的。我还通过添加缺少的语言文本来修复此消息。
【解决方案3】:

这对我有帮助

转到 application/config/autoload.php 并在那里添加“Security”帮助类。

$autoload['helper'] = array('security');

或者在你的表单验证之前添加这个

$this->load->helper('security');

【讨论】:

    【解决方案4】:

    您可以在set_rules 中设置错误信息

    $this-&gt;form_validation-&gt;set_rules('captcha', 'Captcha', 'callback_check_captcha',

    array('check_captcha' =&gt; 'text dont match captcha'));

    【讨论】:

      【解决方案5】:

      在您的语言文件中添加一个条目,其中包含错误消息(yourfieldname)内的部分名称 - 这解决了问题

      【讨论】:

        【解决方案6】:

        即使问题已经得到解答,还有可能导致相同错误消息的另一个错误:

        如果您调用您的回调 checkCaptcha,它将不起作用。始终首选像 Codeigniter/General Topics/PHP 样式指南中推荐的 check_captcha 这样的名称。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-05
          • 2015-12-16
          • 2015-12-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多