我是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;
}
}
xss_clean不再是Codeingtore 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');
大家好,我在hmvc codeIgniter中找到了使用call_backs的解决方案。我想为其他人发布解决方案。
解决方案:
1.在库文件夹中创建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);
}
}
- 然后将
if ($this->form_validation->run() == FALSE)
更改为if ($this->form_validation->run($this) == FALSE)
这就是所有人
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
用于将来的搜索。当出现此错误时,您需要检查两件3事情。
-
如果回调函数的名称与相同,请检查其名称
$this->form_validation->set_rules('word','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.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; }
-
将此文件添加到您的应用程序/库中/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&索引=8
供参考。
此错误的原因是您没有加载安全帮助程序以下是启用安全帮助程序的方法使用config文件夹中的autoload.php,或者您可以直接加载帮助程序,如本文最后一行所述
如果您确实需要它,请访问application/config/autoload.php:
$autoload['helper']=数组('security'(;或者,在表单验证之前$this->load->helper('security'(;
在对文件application/config/autoload.php
进行帮助程序安全检查之前在库中添加form_validation
$autoload['libraries'] = array('form_validation');
别忘了添加安全
$autoload['helper'] = array('security');
试试