我的目标涉及两个步骤
第1步
username
和password
值第2步
对于上面的场景,我的控制器代码如下:
$this->form_validation->set_rules('userid', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login'); // this works fine if either of one is empty
}
else
{
// I query the database and check if valid or not
if(<not_valid>)
{
// but how to pass the "Login Failed" message in this code block?
/* I'm assuming that if I somehow make "$this->form_validation->run()"
return false, then the message can be passed to the view */
$this->load->view('login');
}
else
{
$this->load->view('dashboard');
}
}
echo validation_errors();
<div class="form-group">
<label for="userid" class="control-label">Enter User ID</label>
<input type="text" id="userid" name="userid"
value="<?php echo set_value('userid'); ?>" class="form-control"
placeholder="Email Address"/>
</div>
<div class="form-group">
<label for="password" class="control-label">Enter Password</label>
<input type="password" id="password" name="password" class="form-control"
placeholder="Secret Password"/>
</div>
我知道回调但是如何为两个字段(用户名和密码)触发一个回调?如果我这样做:
$this->form_validation->set_rules('userid', 'Username', 'callback_check_valid');
$this->form_validation->set_rules('password', 'Password', 'callback_check_valid');
这是否意味着我必须自己手动检查这两个字段是否空虚?这样的事情:?
public function check_valid($un, $pw)
{
if(trim($un) == '' || trim($pw) == '')
{
return false;
}
else
{
// check for valid login
}
}
答案 0 :(得分:0)
public function check_valid() {
$un = $_POST['username'];
$pw = $_POST['password'];
if(trim($un) == '' || trim($pw) == '') {
return false;
} else {
// check for valid login
}
}
尝试这个逻辑
答案 1 :(得分:0)
试试这个:
if($this->input->post()){
$this->form_validation->set_rules('userid', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run()){
$username = $this->input->post('userid');
$password = $this->input->post('password');
if($this->check_valid($username, $password)){
$this->load->view('dashboard');
}else{
$this->load->view('login');
}
}else{
$this->load->view('login');
}
}
这与我使用codeigniter进行登录检查的方式类似。
您可以从check_valid函数中离开第二个修剪检查,已经修剪了值。