I have a problem with the validation error for CodeIgniter 4, when i click the button, the error message doesn't appear at all, is there something wrong? Please help, I appreciate it Below is the code:
Controller
public function Daftar()
{
// Jika tervalidasi
if ($this->validate([
'nim' => [
'label' => 'NIM',
'rules' => 'required|is_unique[tbl_anggota.nim]',
'errors' => [
'required' => '{field} Masih Kosong!',
'is_unique' => '{field} NIM Sudah Terdaftar!',
]
],
'nama' => [
'label' => 'Nama',
'rules' => 'required',
'errors' => [
'required' => '{field} Masih Kosong!',
]
],
'password' => [
'label' => 'Password',
'rules' => 'required',
'errors' => [
'required' => '{field} Masih Kosong!',
]
],
'ulangi_password' => [
'label' => 'Ulangi Password',
'rules' => 'required|matches[password]',
'errors' => [
'required' => '{field} Masih Kosong!',
'matches' => '{field} Password Tidak Sama!',
]
],
])) {
} else {
// Jika tidak tervalidasi
session()->getFlashdata('errors', \Config\Services::validation()->getErrors());
return redirect()->to(base_url('Auth/Login'))->withInput('validation', \Config\Services::validation());
}
}
View
<?php
// Notifikasi Error
$errors = session()->getFlashdata('errors');
if (!empty($errors)) { ?>
<div class="alert alert-danger" role="alert">
<h4>Periksa Entry Form</h4>
<ul>
<?php foreach ($errors as $key => $error) { ?>
<li><?= esc($error) ?></li>
<?php } ?>
</ul>
</div>
<?php } ?>
Thank you so much!!
I've tried look it up but still it's not working
you need to use setFlashData() instead of getFlashData()
while setting error in your controller, so change:
..
session()->getFlashdata('errors', \Config\Services::validation()->getErrors());
..
to
..
session()->setFlashdata('errors', \Config\Services::validation()->getErrors());
..