我的代码中存在问题。我正在使用CI3为我的小项目创建一个简单的登录。我的问题是我在回调验证中有一条错误消息。
这是我在尝试验证表单时收到的错误。
Unable to access an error message corresponding to your field name Password.(check_database)
这里是控制器中的mo代码:
public function index() {
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
$this->form_validation->set_error_delimiters('<div class="error text-red">', '</div>');
if($this->form_validation->run() == FALSE) {
$data = array();
$data['modules'] = $this->flx_lib->moduler($data);
$this->load->view('login', $data);
} else {
//ok
}
}
public function check_database($password) {
$username = $this->input->post($username);
$result = $this->flx_users->validate_user($username, $password);
if($result) {
//ok
return TRUE;
} else {
$this->form_validation->set_message('check_database', 'Invalid username or password');
return FALSE;
}
}
以下是我的观点:
<div class="form-group has-feedback <?php error_exists('username'); ?>">
<input type="text" class="form-control" name="username" placeholder="Username" />
<span class="fa fa-user form-control-feedback "></span>
<?php echo form_error('username'); ?>
</div>
<div class="form-group has-feedback <?php error_exists('password'); ?>">
<input type="password" class="form-control" name="password" placeholder="Password">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
<?php echo form_error('password'); ?>
</div>
你能帮我解决这个问题吗?我正在使用CI3和HMVC
答案 0 :(得分:0)
public function index() {
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database',
array('check_database' => 'Invalid username or password.')
);
$this->form_validation->set_error_delimiters('<div class="error text-red">', '</div>');
if($this->form_validation->run() == FALSE) {
$data = array();
$data['modules'] = $this->flx_lib->moduler($data);
$this->load->view('login', $data);
} else {
//ok
}
}
public function check_database($password) {
$username = $this->input->post($username);
$result = $this->flx_users->validate_user($username, $password);
if($result) {
//ok
return TRUE;
} else {
return FALSE;
}
}
我记录了这一个。有些东西搞砸了。 这来自docs:
$this->form_validation->set_rules('field_name', 'Field Label', 'rule1|rule2|rule3',
array('rule2' => 'Error Message on rule2 for this field_name')
);
答案 1 :(得分:0)
好的,我在代码中发现了错误。实际上它是HMVC表格验证的问题。
这里是正确答案的链接: CodeIgniter - Unable to access an error message corresponding to your field name Password.(pword_check)
Solution:
1. Create MY_Form_validation.php file in libraries folder and paste following code in it.
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);
}
}
And change if ($this->form_validation->run() == FALSE) to if ($this->form_validation->run($this) == FALSE) thats all folks..