这是我的 html [create.php] 的代码 (请忽略其中的“Sampul”输入,因为它是在 enctype="multipart/form-data" 之后添加的)
<div class="container">
<div class="row">
<div class="col-8">
<h2 class="my-3">Form Tambah Data Komik</h2>
<!-- my itu margin sumbu y (vertikal) -->
<?php if (session()->getFlashdata('_ci_validation_errors')) : ?>
<div class="alert alert-danger">
<ul>
<?php foreach (session()->getFlashdata('_ci_validation_errors') as $error) : ?>
<li><?= $error ?></li>
<?php endforeach ?>
</ul>
</div>
<?php endif ?>
<form action="/komik/save" method="post" enctype="multipart/form-data">
<?= csrf_field(); ?>
<!-- csrf menjaga supaya form cuma bisa diinput lewat halaman ini saja -->
<div class="row mb-3">
<label for="judul" class="col-sm-2 col-form-label">Judul</label>
<div class="col-sm-10">
<input type="text" class="form-control <?= $validation->hasError('judul') ? 'is-invalid' : ''; ?>" id="judul" name="judul" autofocus value="<?= old('judul'); ?>">
<!-- autofocus bikin ketika masuk ke page langsung aktif seperti habis di klik -->
<div id="validationServer03Feedback" class="invalid-feedback">
<?= $validation->getError('judul'); ?>
</div>
</div>
</div>
<div class="row mb-3">
<label for="penulis" class="col-sm-2 col-form-label">Penulis</label>
<div class="col-sm-10">
<input type="text" class="form-control <?= $validation->hasError('penulis') ? 'is-invalid' : ''; ?>" id="penulis" name="penulis" value="<?= old('penulis'); ?>">
<div id="validationServer03Feedback" class="invalid-feedback">
<?= $validation->getError('penulis'); ?>
</div>
</div>
</div>
<div class="row mb-3">
<label for="penerbit" class="col-sm-2 col-form-label">Penerbit</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="penerbit" name="penerbit" value="<?= old('penerbit'); ?>">
</div>
</div>
<div class="row mb-3">
<label for="sampul" class="col-sm-2 col-form-label">Sampul</label>
<div class="col-sm-10">
<div class="mb-3">
<input class="form-control <?= $validation->hasError('sampul') ? 'is-invalid' : ''; ?>" type="file" id="sampul" name="sampul">
<div id="validationServer03Feedback" class="invalid-feedback">
<?= $validation->getError('sampul'); ?>
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Tambah Data</button>
</form>
</div>
</div>
</div>
它之前运行良好,正确显示错误消息并在输入中的类内显示“is-invalid”,但是在表单内添加 enctype="multipart/form-data" 后,所有验证都放在里面html 刚刚停止工作(包括旧的用于在验证失败时回调值)。
这是用于此的控制器 获取create.php中的函数
public function create()
{
// session();
//session tidak perlu dipanggil karena kita sudah pasang manual session() dalam BaseController.php
$data = [
'title' => 'Form Tambah Data Komik',
// 'validation' => \Config\Services::validation()
'validation' => session()->getFlashdata('validation') ?? \Config\Services::validation()
];
return view('komik/create', $data);
}
这个是保存表单数据的函数(验证失败则返回create.php)
public function save()
{
//validasi input
if (!$this->validate([
'judul' => [
'rules' => 'required|is_unique[komik.judul]',
'errors' => [
'required' => '{field} komik harus diisi.',
'is_unique' => '{field} komik sudah terdaftar'
]
],
'penulis' => [
'rules' => 'required',
'errors' => [
'required' => '{field} komik harus diisi.'
]
],
'sampul' => [
'rules' => 'uploaded[sampul]|max_size[sampul,1024]|is_image[sampul]|mime_in[sampul,image/jpg,image/jpeg,image/png]',
'errors' => [
'uploaded' => 'Pilih gambar sampul terlebih dahulu',
'max_size' => 'Ukuran gambar terlalu besar',
'is_image' => 'Yang anda pilih bukan gambar',
'mime_in' => 'Yang anda pilih bukan gambar',
]
]
])) {
//kalo gagal validasi dia jalankan line ini
session()->setFlashdata('validation', \Config\Services::validation()); //dari comment YT, bisa jalan
return redirect()->to('/komik/create')->withInput();
}
$slug = url_title($this->request->getVar('judul'), '-', true);
$this->komikModel->save([
'judul' => $this->request->getVar('judul'),
'slug' => $slug,
'penulis' => $this->request->getVar('penulis'),
'penerbit' => $this->request->getVar('penerbit'),
'sampul' => $this->request->getVar('sampul')
]);
session()->setFlashdata('pesan', 'Data berhasil ditambahkan.');
return redirect()->to('/komik');
}
控制器内的验证系统工作正常,但所有错误消息、“无效”和旧的都停止显示。
我只是不明白为什么错误消息根本不再显示在 html 中。 enctype="multipart/form-data" 对这段代码有何影响?谁能给我一个解决方案来解决这个问题?
这个错误你解决了吗? 我也有同样的问题。
谢谢你。