【问题标题】:CodeIgniter - Unable to access an error message corresponding to your field name Password.(pword_check)CodeIgniter - 无法访问与您的字段名称密码对应的错误消息。(pword_check)
【发布时间】:2015-12-16 09:52:34
【问题描述】:

我是 codeIgniter 的新手,我刚开始就被卡住了。我正在使用 HMVC 扩展,在验证时出现以下错误:

无法访问与您的字段名称密码对应的错误消息。(pword_check)

任何帮助将不胜感激

代码:

public function submit()
{


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

    $this->form_validation->set_rules('username', 'Username', 'required|max_length[30]|xss_clean');
    $this->form_validation->set_rules('pword', 'Password', 'required|max_length[30]|callback_pword_check|xss_clean');

    if ($this->form_validation->run() == FALSE)
    {
        $this->login();
    }
    else
    {
        echo 'Success'; die();
    }
}

public function pword_check($str)
{

    if ($str == 'test')
    {
        $this->form_validation->set_message('pword_check', 'The %s field can not be the word "test"');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

【问题讨论】:

  • 你使用哪个 CI 版本???
  • 我正在使用 CodeIgniter 版本 3.0.1

标签: php codeigniter validation hmvc


【解决方案1】:

在对文件 application/config/autoload.php 进行帮助安全检查之前 在库中添加 form_validation

$autoload['libraries'] = array('form_validation');

别忘了添加安全性

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

试试看

【讨论】:

    【解决方案2】:

    用于将来的搜索。当出现此错误时,您需要检查两件事 3

    1. 检查回调函数的名称是否与

      $this->form_validation->set_rules('pword', 'Password', 'required|max_length[30]|callback_pword_check

      public function pword_check($str){                                              
        if ($str == 'test'){
          $this->form_validation->set_message('pword_check', 'The %s field can not be the word "test"');
          return FALSE;
        }
        else{
          return TRUE;
        }                                                                              
      }
      

      *公共函数pword_check($str){

      *set_message('pword_check', '%s 字段...

    2. 在 codeigniter 2.X 中你可以这样做

      $this->form_validation->run($this) == TRUE

      在 3.x 中应该是这样的

      $this->form_validation->run() == TRUE

      你需要在__construct()上添加这两行代码

      function __construct(){                                     
        parent::__construct();                                                      
        $this->load->library('form_validation');
        $this->form_validation->CI =& $this;                                           
      } 
      
    3. 将此文件添加到您的应用程序/库/MY_Form_validation.php

      <?php                                                                
      class MY_Form_validation extends CI_Form_validation{                                     
          public $CI;                                                                                               
      } 
      

    干杯!见https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/src/f77a3fc9a6fd?at=codeigniter-3.x

    https://www.youtube.com/watch?v=pp4Y_bIhASY&list=PLBEpR3pmwCayNcTCUWlUToK4qIQfFQCCm&index=8

    供参考。

    【讨论】:

      【解决方案3】:
      public function pword_check($str)
      {
      
          if ($str == 'test')
          {
              $this->form_validation->set_message(  __FUNCTION__ , 'The %s field can not be the word "test"');
              return FALSE;
          }
          else
          {
              return TRUE;
          }
      }
      

      // 效果很好

      https://github.com/bcit-ci/CodeIgniter/issues/3908

      【讨论】:

      • 不鼓励仅使用代码回答,因为它们没有为未来的读者提供太多信息,请对您所写的内容提供一些解释
      【解决方案4】:

      这个错误的原因是 您没有加载安全助手以下是启用安全助手的方式 在配置文件夹中使用 autoload.php 或者您可以直接加载本文最后一行提到的帮助程序

      如果你真的需要它,请转到 application/config/autoload.php :

      $autoload['helper'] = array('security'); 或者,在您的表单验证之前 $this->load->helper('security');

      【讨论】:

        【解决方案5】:

        大家好,我在 hmvc codeIgniter 中找到了使用 call_backs 的解决方案。 我想为其他人发布解决方案。

        解决方案:

        1.在libraries文件夹中创建MY_Form_validation.php文件并粘贴以下代码。

         if (!defined('BASEPATH')) exit('No direct script access allowed');
            class MY_Form_validation extends CI_Form_validation
            {
        
            function run($module = '', $group = '')
            {
               (is_object($module)) AND $this->CI = &$module;
                return parent::run($group);
            }
        }
        

        1. if ($this-&gt;form_validation-&gt;run() == FALSE) 更改为if ($this-&gt;form_validation-&gt;run($this) == FALSE) 就是这样......

        【讨论】:

        • 添加到 CI 3.0.x 中的库中,@kev_m
        • 我们能解释一下为什么会这样吗?顺便感谢一百万,救了我的命!
        • 百万thanx,只是一个建议,你不需要创建库,只需在运行函数中传递 $this 就可以了$this-&gt;form_validation-&gt;run($this)
        【解决方案6】:

        xss_clean 不再是 Codeingitore 3 中表单验证的一部分

        只需将xss_clean 从您的验证机制中删除

        $this->form_validation->set_rules('pword', 'Password', 'required|max_length[30]|callback_pword_check');
        

        如果您确实需要应用该规则,您现在还应该加载 Security Helper,其中包含 xss_clean() 作为常规函数,因此也可以用作验证规则。

        转到application/config/autoload.php :

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

        或者,在您的表单验证之前

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

        【讨论】:

        • 感谢您宝贵的时间。我已经尝试删除 xss_clean 东西,我也自动加载了安全帮助程序,但密码字段仍然显示错误消息。
        • 这是一个非常简单的代码我不知道是什么导致了这个错误。
        • 将您的消息更改为$this-&gt;form_validation-&gt;set_message('pword_check', "The %s field can not be the word test"); 并检查
        • 我将消息更改为您的消息,但没有帮助,再次显示相同的错误(无法访问错误消息)。
        • 这段代码看起来不错。我的问题是其他部分
        猜你喜欢
        • 1970-01-01
        • 2012-05-23
        • 2022-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-21
        相关资源
        最近更新 更多