【发布时间】:2016-11-08 07:32:56
【问题描述】:
我尝试从 Codeigniter 扩展 Form_validation 库,但没有成功。
我在这个扩展类中有两个我想要的功能,但是当验证运行时,我会在日志中收到这些消息:
DEBUG - 06-07-2016 11:20:33 --> 找不到验证规则:checkAccountnameForUpdate 调试 - 06-07-2016 11:20:33 --> 找不到验证规则:checkEmailForUpdate
这是我的扩展类,放在
应用程序/库
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
public function __construct($rules = array())
{
// Pass the $rules to the parent constructor.
parent::__construct($rules);
// $this->CI is assigned in the parent constructor, no need to do it here.
}
public function checkAccountnameForUpdate($accountname, $id)
{
return 1;
}
public function checkEmailForUpdate($email, $id)
{
return 1;
}
}
这是我的规则:
$this->form_validation->set_rules('editAccountname', 'Brugernavn', 'required|checkAccountnameForUpdate[hiddenField]');
$this->form_validation->set_rules('editEmail', 'Email', 'required|checkEmailForUpdate[hiddenField]');
$this->form_validation->set_message('checkAccountnameForUpdate', 'Test2');
$this->form_validation->set_message('checkEmailForUpdate', 'Test');
// hiddenField is the name of a hiddenField containing the users ID.
我用谷歌搜索了一些不同的东西。我不知道为什么它不起作用。
编辑 1:
我已经尝试替换
$this->form_validation->set_rules('editAccountname', 'Brugernavn', 'required|checkAccountnameForUpdate[hiddenField]');
$this->form_validation->set_rules('editEmail', 'Email', 'required|checkEmailForUpdate[hiddenField]');
到
$this->form_validation->set_rules('editAccountname', 'Brugernavn', 'required|checkAccountnameForUpdate');
$this->form_validation->set_rules('editEmail', 'Email', 'required|checkEmailForUpdate');
没有变化。
【问题讨论】:
-
你在使用 CodeIgniter 3 吗?
-
您期望在您的函数中传递 2 个变量,但
set_rules每个规则只发送一个。在这种情况下$this->input->post('editAccountName'),您还缺少两个函数的$this->form_validation->set_message()。 -
@AniruddhaChakraborty - 是 CI3。
-
@Jordy - 我有 set_message()。它已包含在主帖中。但我读到我可以通过这种方式传递两个变量。它只计算回调吗?
-
我使用了您的确切方法,包括传递两个参数,并且效果很好。我不明白你为什么会遇到问题。没有其他错误消息?
标签: php codeigniter validation twitter-bootstrap-3 continuous-integration