【发布时间】:2023-03-31 05:31:01
【问题描述】:
我的目标包括两个步骤
第 1 步
- 首先检查空的
usernameANDpassword值 - 如果为空,则返回登录表单,说明空值 被发现
第 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');
}
}
HTML
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>
PS
我知道回调,但是如何为两个字段(用户名和密码)触发一个回调?如果我这样做:
$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
}
}
【问题讨论】:
-
你需要改变你的逻辑,似乎codeigniters回调无法发送2个参数......但你可以使用
$_POST;) -
@Kyslik 事实证明,我可以将两个参数发送到回调函数
check_valid():)
标签: php codeigniter validation