我在登录表单中使用了 Codeigniter 4 表单验证。此外,验证表单运行良好或良好。加载登录页面时,在该导入字段之前也显示错误。在下面,我插入了与登录部分相关的控制器和视图代码。我检查了不同的方法,但我没有办法解决这个问题。请指导我解决它。
用户控制器:
<?php namespace App\Controllers;
use App\Models\UserModel;
class User extends BaseController
{
public function index()
{
echo "User index";
// Load views
// echo view('static/header');
// echo view('front/all');
// echo view('static/footer');
}
public function login()
{
helper('form');
$val = $this->validate([
'username' => 'required',
'password' => 'required',
]);
if (!$val)
{
echo view('front/login', [
'validation' => $this->validator
]);
}
else
{
return redirect()->to('/');
}
}
public function logout()
{
}
public function register()
{
// Load views
echo view('front/register');
}
}
login.php(登录页面视图):
<body>
<?= \Config\Services::validation()->listErrors(); ?>
<div class="limiter">
<div class="container-login100">
<div class="wrap-login100 p-b-160 p-t-50">
<?php echo form_open(base_url('user/login'),array('class'=>'login100-form validate-form')); ?>
<span class="login100-form-title p-b-43">
Login Form
</span>
<div class="wrap-input100 rs1 validate-input">
<input class="input100" type="text" name="username">
<span class="label-input100">User Name</span>
</div>
<div class="wrap-input100 rs2 validate-input">
<input class="input100" type="password" name="password">
<span class="label-input100">Password</span>
</div>
<div class="container-login100-form-btn">
<button class="login100-form-btn" type="submit">
Login
</button>
</div>
<div class="text-center w-full p-t-23">
<a href="#" class="txt1">
Forget Password?
</a>
</div>
<div class="text-center w-full p-t-23">
<a href="<?php echo base_url('user/register'); ?>" class="txt1">
SignUp
</a>
</div>
</form>
</div>
</div>
</div>
</body>
输出(登录页面首次访问): localhost/user/login