'how to validate form data in codeigniter 4
i am new to codeigniter 4, i am currently building a registration form. when i submit reg form it give error of required fields even if i fill all fields with correct data.
here is my code snippet
function register() {
$data = [];
helper(['form']);
if($this->request->getMethod() == "post"){
$validation = \Config\Services::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');
}
this is the registration form i am trying to validate.
<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>
and here is the output i am getting even if i fill all fields.
Solution 1:[1]
If you want to always validate data going into that model, you might want to consider doing the validation inside the model:
https://codeigniter.com/user_guide/models/model.html#validating-data
In case you want to validate the data outside the model you have to tell the validation service on where the data is, because it can be POST, GET or even just an array that you have from something else.
In your case you need to validate the data with your request.
https://codeigniter.com/user_guide/libraries/validation.html?highlight=validation#withrequest
So your validation code should be something like:
if($validation->withRequest($this->request)->run()){ }
This will look for the data in both GET and POST.
If you want to specify and only use POST
if($validation->withRequest($this->request->getPost())->run()){ }
Solution 2:[2]
try this style
function register()
{
$data = [];
helper(['form']);
if ($this->request->getMethod() == "post") {
$validation = \Config\Services::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');
}
just put your rules inside an array and pass it to controller validate function..
i hope it's work
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | nadim achmad |