php 错误验证未出现CodeIgniter 4

31moq8wy  于 2023-06-28  发布在  PHP
关注(0)|答案(1)|浏览(89)

我遇到了CodeIgniter 4的验证错误问题,当我单击按钮时,错误消息根本没有出现,是否有问题?请帮忙,我很感激下面是代码:

控制器

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());
        }
    }

查看

<?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 } ?>

谢谢你!!
我试着查了一下,但还是不行

sulc1iza

sulc1iza1#

你需要使用setFlashData()而不是getFlashData()在你的控制器中设置错误,所以改变:

..
session()->getFlashdata('errors', \Config\Services::validation()->getErrors());
..

..
session()->setFlashdata('errors', \Config\Services::validation()->getErrors());
..

相关问题