如何验证代码点火器4中的表单数据



我是codeigniter 4的新手,目前正在构建一个注册表单。当我提交注册表单时,即使我用正确的数据填写了所有字段,它也会给出所需字段的错误。

这是我的代码片段

function register() {
$data = [];
helper(['form']);
if($this->request->getMethod() == "post"){
$validation =  ConfigServices::validation();
$validation->setRules([
"firstname" => ["label" => "First Name", "rules" => "required|min_length[3]|max_length[20]"],
"lastname" => ["label" => "Last Name", "rules" => "required|min_length[3]|max_length[20]"],
"email" => ["label" => "Email", "rules" => "required|min_length[3]|max_length[20]|valid_email|is_unique[users.email]"],
"password" => ["label" => "Password", "rules" => "required|min_length[8]|max_length[20]"],
"password_confirm" => ["label" => "Confirm Password", "rules" => "matches[password]"],
]);
if($validation->run()){

$user = new UserModel();
$userdata = [
"firstname" => $this->request->getVar("firstname"),
"lastname" => $this->request->getVar("lastname"),
"email" => $this->request->getVar("email"),
"password_confirm" => $this->request->getVar("password_confirm"),
];
$user->save($userdata);
$session = session();
$session->setFlashData("success", "Successful Registration");
return redirect()->to('/');

}else{
$data["validation"] = $validation->getErrors();
}
}

echo view('templates/header', $data);
echo view('register');
echo view('templates/footer');
}

这是我要验证的登记表。

<form class="" action="/register" method="post">
<div class="row">
<div class="col-12 col-sm-6">
<div class="form-group">
<label for="firstname">First Name</label>
<input type="text" class="form-control" name="firstname" id="firstname" value="<?= set_value('firstname') ?>">
<small class="text-danger"><?= isset($validation) ? $validation['firstname'] : null;  ?></small>
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label for="lastname">Last Name</label>
<input type="text" class="form-control" name="lastname" id="lastname" value="<?= set_value('lastname'); ?>">
<small class="text-danger"><?= isset($validation) ? $validation['lastname'] : null ;  ?></small>
</div>
</div>
<div class="col-12">
<div class="form-group">
<label for="email">Email address</label>
<input type="text" class="form-control" name="email" id="email" value="<?= set_value('email') ?>">
<small class="text-danger"><?= isset($validation) ? $validation['email'] : null ;  ?></small>
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" id="password" value="">
<small class="text-danger"><?= isset($validation) ?  $validation['password'] :null ; ?></small>
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label for="password_confirm">Confirm Password</label>
<input type="password" class="form-control" name="password_confirm" id="password_confirm" value="">
<small class="text-danger"><?= isset($validation) ?  $validation['password_confirm'] :null ; ?></small>
</div>
</div>
</div>
<div class="row">
<div class="col-12 col-sm-4">
<button type="submit" class="btn btn-primary">Register</button>
</div>
<div class="col-12 col-sm-8 text-right">
<a href="/">Already have an account</a>
</div>
</div>
</form>

这是我得到的输出,即使我填充了所有字段。

如果您想始终验证进入该模型的数据,您可能需要考虑在模型内部进行验证:

https://codeigniter.com/user_guide/models/model.html#validating-数据

如果你想在模型之外验证数据,你必须告诉验证服务数据在哪里,因为它可以是POST、GET,甚至只是你从其他地方得到的数组。

在您的情况下,您需要根据请求验证数据。

https://codeigniter.com/user_guide/libraries/validation.html?highlight=validation#withrequest

所以你的验证码应该是这样的:

if($validation->withRequest($this->request)->run()){ }

这将在GET和POST中查找数据。

如果要指定并仅使用POST

if($validation->withRequest($this->request->getPost())->run()){ }

试试这种风格的

function register()
{
$data = [];
helper(['form']);
if ($this->request->getMethod() == "post") {
$validation =  ConfigServices::validation();
$rules = [
"firstname" => [
"label" => "First Name", 
"rules" => "required|min_length[3]|max_length[20]"
],
"lastname" => [
"label" => "Last Name", 
"rules" => "required|min_length[3]|max_length[20]"
],
"email" => [
"label" => "Email", 
"rules" => "required|min_length[3]|max_length[20]|valid_email|is_unique[users.email]"
],
"password" => [
"label" => "Password", 
"rules" => "required|min_length[8]|max_length[20]"
],
"password_confirm" => [
"label" => "Confirm Password", 
"rules" => "matches[password]"
]
];
if ($this->validate($rules)) {
$user = new UserModel();
$userdata = [
"firstname" => $this->request->getVar("firstname"),
"lastname" => $this->request->getVar("lastname"),
"email" => $this->request->getVar("email"),
"password_confirm" => $this->request->getVar("password_confirm"),
];
$user->save($userdata);
$session = session();
$session->setFlashData("success", "Successful Registration");
return redirect()->to('/');
} else {
$data["validation"] = $validation->getErrors();
}
}

echo view('templates/header', $data);
echo view('register');
echo view('templates/footer');
}

只需将规则放入数组中,然后将其传递给控制器验证函数即可。。

我希望这是工作

相关内容

  • 没有找到相关文章

最新更新