Code:
<!DOCTYPE html>
<html>
<head>
<title>Codeigniter 4 Form Validation Example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<br>
<?= \Config\Services::validation()->listErrors(); ?>
<div class="row">
<div class="col-md-9">
<form action="<?php echo base_url('contact/create') ?>" method="post" accept-charset="utf-8">
<div class="form-group">
<label for="formGroupExampleInput">Name</label>
<input type="text" name="name" class="form-control" id="formGroupExampleInput" placeholder="Please enter name">
</div>
<div class="form-group">
<label for="email">Email Id</label>
<input type="text" name="email" class="form-control" id="email" placeholder="Please enter email id">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea name="message" class="form-control"></textarea>
</div>
<div class="form-group">
<button type="submit" id="send_form" class="btn btn-success">Submit</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
**************************
My controller
<?php namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\ContactModel;
class Contact extends Controller
{
public function index()
{
return view('contact');
}
public function create()
{
helper(['form', 'url']);
$val = $this->validate([
'name' => 'required',
'email' => 'required',
'message' => 'required',
]);
$model = new ContactModel();
if (!$val)
{
echo view('contact', [
'validation' => $this->validator
]);
}
else
{
$model->save([
'name' => $this->request->getVar('name'),
'email' => $this->request->getVar('email'),
'message' => $this->request->getVar('message'),
]);
echo view('success');
}
}
}