Codeigniter 4 Tutorials
- Codeigniter 4
- Application Run
- Application Structure
- Configuration files
- Errors
- Controller
- View
- Model
- How to remove index.php from URL
- Route
- CodeIgniter URLs
- Helper
- Session
- Global Functions and Constants
- Logging Information in CI4
- Error Handling
- Signin / SignUp
- Login / Register
- CRUD
- Crud 1
- Form Validation
- Data Table
- Pagination
- File Upload
- Show Data Using Ajax
- Select2 AJAX Autocomplete Search
- Curl Post
- Rest API
- Weblink
Home » Codeigniter 4 »
How to create Form Validation in Codeigniter 4?
Table
File name : users.php
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(50) NOT NULL,
`phone` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Model
Create app/Models/FormModel.php
File name : FormModel.php
<?php
namespace App\Models;
use CodeIgniter\Model;
class FormModel extends Model
{
protected $table = 'users';
protected $primaryKey = 'id';
protected $allowedFields = ['name', 'email', 'phone'];
}
Create Controller
File name : index.php
<?php
namespace App\Controllers;
use App\Models\FormModel;
use CodeIgniter\Controller;
class FormController extends Controller
{
public function create() {
return view('contact_form');
}
public function formValidation() {
helper(['form', 'url']);
$input = $this->validate([
'name' => 'required|min_length[3]',
'email' => 'required|valid_email',
'phone' => 'required|numeric|max_length[10]'
]);
$formModel = new FormModel();
if (!$input) {
echo view('contact_form', [
'validation' => $this->validator
]);
} else {
$formModel->save([
'name' => $this->request->getVar('name'),
'email' => $this->request->getVar('email'),
'phone' => $this->request->getVar('phone'),
]);
return $this->response->redirect(site_url('/submit-form'));
}
}
}
Create Routes
File name : index.php
$routes->get('contact-form', 'FormController::create');
$routes->post('submit-form', 'FormController::formValidation');
Create Form & Show Server Side Errors
File name : app/Views/contact_form.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Codeigniter 4 Form Validation Example - positronx.io</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<style>
.container {
max-width: 550px;
}
</style>
</head>
<body>
<div class="container mt-5">
<?php $validation = \Config\Services::validation(); ?>
<form method="post" action="<?php echo base_url('/submit-form') ?>">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control">
<!-- Error -->
<?php if($validation->getError('name')) {?>
<div class='alert alert-danger mt-2'>
<?= $error = $validation->getError('name'); ?>
</div>
<?php }?>
</div>
<div class="form-group">
<label>Email</label>
<input type="text" name="email" class="form-control">
<!-- Error -->
<?php if($validation->getError('email')) {?>
<div class='alert alert-danger mt-2'>
<?= $error = $validation->getError('email'); ?>
</div>
<?php }?>
</div>
<div class="form-group">
<label>Phone</label>
<input type="text" name="phone" class="form-control">
<!-- Error -->
<?php if($validation->getError('phone')) {?>
<div class='alert alert-danger mt-2'>
<?= $error = $validation->getError('phone'); ?>
</div>
<?php }?>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block">Submit</button>
</div>
</form>
</div>
</body>
</html>
Start the Application
File name : index.php
php spark serve
File name : index.php