Hi, I'm a newbie and I want to ask you something about validation.
I just created some rules for form validation.
This is my Controller :
Code:
public function create()
{
if (!$this->validate([
'name' => [
'rules' => 'required',
'errors' => [
'required' => 'Field Name is required.',
]
]
])) {
$validation = $this->validation;
session()->setFlashdata('fail', 'something wrong, Check your form input!');
return redirect()->back()->withInput()->with('validation', $validation);
} else {
dd("OK");
}
}
This is my View :
Code:
<form action="/Home/create" method="post" enctype="multipart/form-data">
<?= csrf_field(); ?>
<div class="mb-3">
<label for="name" class="form-label">Nama</label>
<input type="text" class="form-control <?= ($validation->hasError('name')) ? 'is-invalid' : ''; ?>" id="name" name="name" value="<?= old('name'); ?>" autofocus autocomplete="off">
<div class="invalid-feedback">
<?= $validation->getError('name'); ?>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
But when I tried to submit the form with the empty field and I dd($validation->getError('name')) the return is empty error data.
My PHP Version : 8.2.12
My CodeIgniter Version : 4.5.5
Please Help Me, Master.