英文:
Error validation does not appear CodeIgniter 4
问题
我在CodeIgniter 4的验证错误方面遇到了问题,当我点击按钮时,错误消息根本不会出现,有什么问题吗?请帮忙,我非常感激。
以下是代码:
控制器
public function Daftar()
{
// 如果验证通过
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
',
'errors' => [
'required' => '{field} Masih Kosong!',
'matches' => '{field} Password Tidak Sama!',
]
],
])) {
} else {
// 如果验证未通过
session()->getFlashdata('errors', \Config\Services::validation()->getErrors());
return redirect()->to(base_url('Auth/Login'))->withInput('validation', \Config\Services::validation());
}
}
视图
<?php
// 错误通知
$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 } ?>
非常感谢!!
我尝试查找解决方法,但仍然不起作用。
英文:
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
',
'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
答案1
得分: 0
你需要在控制器中设置错误时使用[setFlashData()][1]
而不是getFlashData()
,所以请将以下代码更改为:
..
session()->setFlashdata('errors', \Config\Services::validation()->getErrors());
..
英文:
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());
..
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论