Codeigniter 4 $validation 不显示 listErrors() 并显示警报警报危险

Codeigniter 4 $validation not showing listErrors() and showing a alert alert-danger

我不知道这个错误是什么,但我确定那是正确的代码,我没有点击按钮就显示了警报,在我的教程中,如果用户点击该按钮,就会显示警报。This is showing when i press F5 感谢阅读这个问题我希望有人能帮助我,我是初学者所以希望你们理解。

我的控制器Subbag.php

<?php

namespace App\Controllers;

use App\Models\KasubbagModel;
use CodeIgniter\Controller;


class Subbag extends BaseController
{
    protected $Kasubbag;
    public function __construct()
    {
        $this->KasubbagModel = new KasubbagModel();
    }

    public function kasubbag()
    {
        $Kasubbag = $this->KasubbagModel->findAll();

        $data = [
            'title' => 'Data Kasubbag | SIKODOK',
            'Kasubbag' => $Kasubbag
        ];

        // $KasubbagModel = new KasubbagModel();

        return view('page/kasubbag', $data);
    }
    public function Create()
    {
        session();
        $data = [
            'title' => 'Tambah Data | SIKODOK',
            'validation' => \Config\Services::validation()
        ];

        return view('page/createSubbag', $data);
    }
    public function save()
    {
        if (!$this->validate([
            'nama_kasubbag' => 'required|is_unique[tbl_kasubbag.nama_kasubbag]'
        ])) {
            $validation = \Config\Services::validation();

            return redirect()->to('/Create')->withInput()->with('validation', $validation);
        }


        $this->KasubbagModel->save([
            'nama_kasubbag' => $this->request->getVar('nama_kasubbag'),
            'sub_bagian' => $this->request->getVar('sub_bagian'),
            'NIP' => $this->request->getVar('NIP')
        ]);


        session()->setFlashdata('pesan', 'Sesuai Perintah Anda!');


        return redirect()->to('Kasubbag');
    }
}

这是我创建的视图输入Subbag.php

<?= $this->extend('template/template'); ?>


<?= $this->section('content'); ?>
<aside class="main-sidebar elevation-4 sidebar-dark-olive">
  <!-- Brand Logo -->
  <a href="<?= base_url(); ?>" class="brand-link">
    <img src="<?= base_url(); ?>/template/dist/img/AdminLTELogo.png" alt="SIKODOK Logo" class="rounded-circle" style="opacity: .8; width:25%; height:auto;">
    <span class="brand-text font-weight-light">SIKODOK</span>
  </a>

  <!-- Sidebar -->
  <div class="sidebar os-host os-host-resize-disabled os-host-scrollbar-vertical-hidden os-host-transition os-host-scrollbar-horizontal-hidden os-theme-light">
    <div class="os-resize-observer-host observed">
      <div class="os-resize-observer" style="left: 0px; right: auto;"></div>
    </div>
    <div class="os-size-auto-observer observed" style="height: calc(100% + 1px); float: left;">
      <div class="os-resize-observer"></div>
    </div>
    <div class="os-content-glue" style="margin: 0px -8px; width: 249px; height: 864px;"></div>
    <div class="os-padding">
      <div class="os-viewport os-viewport-native-scrollbars-invisible">
        <div class="os-content" style="padding: 0px 8px; height: 100%; width: 100%;">
          <!-- Sidebar user panel (optional) -->
          <div class="user-panel mt-5 pb-3 mb-2 d-flex">

            <div class="info">
              <a href="#" class="d-block">Selamat Datang. . . </a>
            </div>
          </div>

          <!-- SidebarSearch Form -->
          <div class="form-inline">
            <div class="input-group" data-widget="sidebar-search">
              <input class="form-control form-control-sidebar" type="search" placeholder="Search" aria-label="Search">
              <div class="input-group-append">
                <button class="btn btn-sidebar">
                  <i class="fas fa-search fa-fw"></i>
                </button>
              </div>
            </div>
            <div class="sidebar-search-results">
              <div class="list-group"><a href="#" class="list-group-item">
                  <div class="search-title"><strong class="text-light"></strong>N<strong class="text-light"></strong>o<strong class="text-light"></strong> <strong class="text-light"></strong>e<strong class="text-light"></strong>l<strong class="text-light"></strong>e<strong class="text-light"></strong>m<strong class="text-light"></strong>e<strong class="text-light"></strong>n<strong class="text-light"></strong>t<strong class="text-light"></strong> <strong class="text-light"></strong>f<strong class="text-light"></strong>o<strong class="text-light"></strong>u<strong class="text-light"></strong>n<strong class="text-light"></strong>d<strong class="text-light"></strong>!<strong class="text-light"></strong></div>
                  <div class="search-path"></div>
                </a></div>
            </div>
          </div>

          <!-- Sidebar Menu -->
          <nav class="mt-2">
            <ul class="nav nav-pills nav-sidebar flex-column nav-flat nav-legacy nav-compact nav-child-indent nav-collapse-hide-child" data-widget="treeview" role="menu" data-accordion="false">
              <!-- Add icons to the links using the .nav-icon class
               with font-awesome or any other icon font library -->
              <li class="nav-item ">
                <a href="#" class="nav-link ">
                  <i class="nav-icon fas fa-tachometer-alt"></i>
                  <p>
                    Menu
                    <i class="right fas fa-angle-left"></i>
                  </p>
                </a>
                <ul class="nav nav-treeview">
                  <li class="nav-item">
                    <a href="<?= base_url(); ?>" class="nav-link ">
                      <i class="far fa-circle nav-icon"></i>
                      <p>Home</p>
                    </a>
                  </li>
                  <li class="nav-item">
                    <a href="<?= base_url('/Feedback'); ?>" class="nav-link active">
                      <i class="far fa-circle nav-icon"></i>
                      <p>Feedback</p>
                    </a>
                  </li>

                </ul>
              </li>






              <li class="nav-header">EXAMPLES</li>
              <li class="nav-item menu-open">
                <a href="#" class="nav-link active">
                  <i class="nav-icon fas fa-table"></i>
                  <p>
                    Tables
                    <i class="fas fa-angle-left right"></i>
                  </p>
                </a>
                <ul class="nav nav-treeview">

                  <li class="nav-item">
                    <a href="<?= base_url('Data'); ?>" class="nav-link">
                      <i class="far fa-circle nav-icon"></i>
                      <p>Tabel Data</p>
                    </a>
                  </li>

                </ul>
              </li>
              <li class="nav-item">
                <a href="<?= base_url('/Gallery'); ?>" class="nav-link">
                  <i class="nav-icon far fa-image"></i>
                  <p>
                    Gallery
                  </p>
                </a>
              </li>
            </ul>
          </nav>
          <!-- /.sidebar-menu -->
        </div>
      </div>
    </div>
    <div class="os-scrollbar os-scrollbar-horizontal os-scrollbar-unusable os-scrollbar-auto-hidden">
      <div class="os-scrollbar-track">
        <div class="os-scrollbar-handle" style="width: 100%; transform: translate(0px);"></div>
      </div>
    </div>
    <div class="os-scrollbar os-scrollbar-vertical os-scrollbar-unusable os-scrollbar-auto-hidden">
      <div class="os-scrollbar-track">
        <div class="os-scrollbar-handle" style="height: 100%; transform: translate(0px);"></div>
      </div>
    </div>
    <div class="os-scrollbar-corner"></div>
  </div>
  <!-- /.sidebar -->
</aside>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <div class="container-fluid">
      <div class="row mb-2">
        <div class="col-sm-6">
          <h1>Tambahkan Data Kasubbag</h1>
        </div>
        <div class="col-sm-6">
          <ol class="breadcrumb float-sm-right">
            <li class="breadcrumb-item"><a href="#"></a></li>
          </ol>
        </div>
      </div>
    </div><!-- /.container-fluid -->
  </section>

  <?php if (isset($validation)) { ?>
    <div class="alert alert-danger" role="alert">
      <?= $validation->listErrors(); ?>
    <?php } ?>
    <!-- Main content -->
    <section class="content">
      <div class="container">
        <div class="col-md-12">
          <div class="card card-dark">
            <div class="card-header">
              <h3 class="card-title">Tambahkan</h3>
            </div>
            <form action="/Subbag/save" method="post">
              <?= csrf_field(); ?>
              <div class="card-body">
                <div class="form-group">
                  <label for="nama_kasubbag">Nama Kasubbag</label>
                  <input type="text" id="nama_kasubbag" class="form-control" name="nama_kasubbag" autofocus>
                </div>
                <div class="form-group">
                  <label for="sub_bagian">Bagian</label>
                  <input type="text" id="sub_bagian" class="form-control" name="sub_bagian">
                </div>
                <!-- <div class="form-group">
                <label for="inputStatus">Status</label>
                <select id="inputStatus" class="form-control custom-select">
                  <option selected disabled>Select one</option>
                  <option>On Hold</option>
                  <option>Canceled</option>
                  <option>Success</option>
                </select>
              </div> -->
                <div class="form-group">
                  <style>
                    input::-webkit-outer-spin-button,
                    input::-webkit-inner-spin-button {
                      -webkit-appearance: none;
                      margin: 0;
                    }

                    /* Firefox */
                    input[type=number] {
                      -moz-appearance: textfield;
                    }
                  </style>
                  <label for="NIP">Nomor Identitas Pegawai </label>
                  <input type="number" placeholder="masukkan nomor 18 digit NIP" id="NIP" class="form-control" name="NIP">
                </div>

              </div>

              <!-- /.card-body -->
          </div>
          <!-- /.card -->
        </div>

      </div>
      <div class="container">
        <div class="col-12">
          <a href="<?= base_url('Kasubbag'); ?>" class="btn btn-secondary">Cancel</a>
          <button type="submit" class="btn btn-success float-right">Tambahkan Data</button>
        </div>
      </div>
      </form>
    </section>
    <!-- /.content -->

    </div>

    <!-- /.content-wrapper -->


    <?= $this->endSection(); ?>

在你的创建方法中,你不需要调用验证服务,尝试使用$this->validator->getErrors(),你的情况可能是这样的:

return redirect()->to('/Create')->withInput()->with('errors', $this->validator->getErrors());

或者可能是这样的:

return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());

而且您不再需要在视图中检查验证,只需检查会话中是否有错误:

<?php if (session()->has('errors')) : ?>
    <ul class="alert alert-danger">
    <?php foreach (session('errors') as $error) : ?>
        <li><?= $error ?></li>
    <?php endforeach ?>
    </ul>
<?php endif ?>