{"id":5971,"date":"2021-05-13T16:53:34","date_gmt":"2021-05-13T16:53:34","guid":{"rendered":"https:\/\/programmingfields.com\/?p=5971"},"modified":"2026-05-02T06:32:19","modified_gmt":"2026-05-02T06:32:19","slug":"form-validation-example-in-codeigniter-4","status":"publish","type":"post","link":"https:\/\/programmingfields.com\/form-validation-example-in-codeigniter-4\/","title":{"rendered":"Form Validation Example in Codeigniter 4 For Beginners"},"content":{"rendered":"\n<p>Codeigniter provides the form helper to validate the form. You can easily validate the inputs like name, email, password, and many more fields. In the entire form validation process, the form will submit to the specified action. While submitting the form to the action, it will check the form inputs as per the specified rules. Here, we will see the step-by-step process of the form validation in Codeigniter 4. Before submitting the form, we have to create a form first. If you are a complete beginner in Codeigniter 4 then you must start with my previous post. In this series, I already started from very scratch from the <a href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-codeigniter-4-for-beginners\/\">CRUD Application in CodeIgniter 4<\/a>. So, without taking more time, let&#8217;s dive into the example by creating a new project in Codeigniter 4.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/programmingfields.com\/form-validation-example-in-codeigniter-4\/#Prerequisites\" >Prerequisites<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/programmingfields.com\/form-validation-example-in-codeigniter-4\/#Create_a_New_Project_For_Form_Validation_in_Codeigniter_4\" >Create a New Project For Form Validation in Codeigniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/programmingfields.com\/form-validation-example-in-codeigniter-4\/#Create_and_Configure_Database_in_Codeigniter_4\" >Create and Configure Database in Codeigniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/programmingfields.com\/form-validation-example-in-codeigniter-4\/#Create_Migration_For_Form_Validation_in_Codeigniter_4\" >Create Migration For Form Validation in Codeigniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/programmingfields.com\/form-validation-example-in-codeigniter-4\/#Create_Model_in_Codeigniter_4\" >Create Model in Codeigniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/programmingfields.com\/form-validation-example-in-codeigniter-4\/#Create_a_Controller_For_Form_Validation_in_Codeigniter_4\" >Create a Controller For Form Validation in Codeigniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/programmingfields.com\/form-validation-example-in-codeigniter-4\/#Create_Routes_For_Form_Validation_in_Codeigniter_4\" >Create Routes For Form Validation in Codeigniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/programmingfields.com\/form-validation-example-in-codeigniter-4\/#Create_a_View_For_Form_Handling\" >Create a View For Form Handling<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/programmingfields.com\/form-validation-example-in-codeigniter-4\/#Check_the_Validation_Result\" >Check the Validation Result<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/programmingfields.com\/form-validation-example-in-codeigniter-4\/#Change_Label_For_Validation_Error_Message_in_Codeigniter_4\" >Change Label For Validation Error Message in Codeigniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/programmingfields.com\/form-validation-example-in-codeigniter-4\/#Set_Custom_Validation_Error_Message\" >Set Custom Validation Error Message<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/programmingfields.com\/form-validation-example-in-codeigniter-4\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prerequisites\"><\/span><strong>Prerequisites<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you want to create a new project in Codeigniter 4 then you must have the following requirements-<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>PHP &gt;= 7.3<\/strong><\/li><li><strong>MySQL (version &gt; 5)<\/strong><\/li><li><strong>Apache\/Nginx Server<\/strong><\/li><li><strong>VS Code Editor<\/strong><\/li><li><strong>Composer<\/strong><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_a_New_Project_For_Form_Validation_in_Codeigniter_4\"><\/span><strong>Create a New Project For Form Validation in Codeigniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For creating the new project, I will be using composer. You can create the Codeigniter 4 project using the composer too. I already shown you in my last tutorial of Codeigniter 4. Hence, open the terminal and hit the below command.<\/p>\n\n\n\n<pre title=\"create new project\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">composer create-project codeigniter4\/appstarter form-validation<\/code><\/pre>\n\n\n\n<p>The above command will create a new folder as I specified the project name. Then inside the folder, it will install the Codeigniter 4 setup and necessary files.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"814\" height=\"531\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-10-08-35-14.png?resize=814%2C531&#038;ssl=1\" alt=\"Create New Project For Form Validation in Codeigniter 4\" class=\"wp-image-5977\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-10-08-35-14.png?w=814&amp;ssl=1 814w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-10-08-35-14.png?resize=300%2C196&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-10-08-35-14.png?resize=768%2C501&amp;ssl=1 768w\" sizes=\"auto, (max-width: 814px) 100vw, 814px\" \/><figcaption><strong>Create New Project in Codeigniter 4<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>After creating the project, let&#8217;s create a database in MySQL.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/how-to-install-codeigniter-4-in-windows-and-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Install CodeIgniter 4 in Windows and Linux<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_and_Configure_Database_in_Codeigniter_4\"><\/span><strong>Create and Configure Database in Codeigniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>I am going to create a new database in MySQL. Hence, you can create a database by hitting the below command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">CREATE DATABASE ci4_form;<\/code><\/pre>\n\n\n\n<p>After creating the database, just open the Codeigniter project into the editor. Then follow the below steps-<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Rename the <strong>env<\/strong> file to <strong>.env<\/strong>.<\/li><li>Change the environment to <strong>development<\/strong> from <strong>production<\/strong>.<\/li><li>Now, under the database section, you will find the default database configuration.<\/li><\/ol>\n\n\n\n<pre title=\".env file - database configuration\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">#--------------------------------------------------------------------\n# DATABASE\n#--------------------------------------------------------------------\n\ndatabase.default.hostname = localhost\ndatabase.default.database = ci4_form\ndatabase.default.username = root\ndatabase.default.password = root\ndatabase.default.DBDriver = MySQLi<\/code><\/pre>\n\n\n\n<p>In the next step, we will create a migration for the users table. Actually, I will be creating a basic user registration form. Then, I will implement the form validation on it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Migration_For_Form_Validation_in_Codeigniter_4\"><\/span><strong>Create Migration For Form Validation in Codeigniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can create a migration for the table schema in Codeigniter 4. I have already shown you in my last Codeigniter 4 tutorial. So, inside the project, open the terminal and hit the below command.<\/p>\n\n\n\n<pre title=\"create migration for users table\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php spark make:migration users --table<\/code><\/pre>\n\n\n\n<p>The above command will create a migration file for the users inside the <strong>app\/Database\/Migrations<\/strong> folder.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"89\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-08-43-44.png?resize=1024%2C89&#038;ssl=1\" alt=\"Create Migration For Users Table\" class=\"wp-image-5983\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-08-43-44.png?resize=1024%2C89&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-08-43-44.png?resize=300%2C26&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-08-43-44.png?resize=768%2C66&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-08-43-44.png?w=1087&amp;ssl=1 1087w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Create Migration For Users Table<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Here, I have added the necessary fields for the form validation. You can take a look, if required then you may add more fields. I am just going to show you the basic concept for the form validation.<\/p>\n\n\n\n<pre title=\"timestamp_Users.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nnamespace App\\Database\\Migrations;\n\nuse CodeIgniter\\Database\\Migration;\n\nclass Users extends Migration\n{\n    public function up()\n    {\n        $this-&gt;forge-&gt;addField([\n            'id' =&gt; [\n                'type' =&gt; 'INT',\n                'auto_increment' =&gt; true\n            ],\n            'name' =&gt; [\n                'type' =&gt; 'VARCHAR',\n                'constraint' =&gt; '30'\n            ],\n            'email' =&gt; [\n                'type' =&gt; 'VARCHAR',\n                'constraint' =&gt; '50'\n            ],\n            'password' =&gt; [\n                'type' =&gt; 'VARCHAR',\n                'constraint' =&gt; '20'\n            ],\n            'phone' =&gt; [\n                'type' =&gt; 'VARCHAR',\n                'constraint' =&gt; '15'\n            ],\n            'address' =&gt; [\n                'type' =&gt; 'TEXT'\n            ],\n            'created_at datetime default current_timestamp',\n            'updated_at datetime default current_timestamp on update current_timestamp'\n        ]);\n        $this-&gt;forge-&gt;addKey('id', true);\n        $this-&gt;forge-&gt;createTable('users');\n    }\n\n    public function down()\n    {\n        $this-&gt;forge-&gt;dropTable('users');\n    }\n}<\/code><\/pre>\n\n\n\n<p>After putting the schema of the users migration, let&#8217;s migrate it using below command.<\/p>\n\n\n\n<pre title=\"migrate tables\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php spark migrate<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"904\" height=\"129\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-09-04-28.png?resize=904%2C129&#038;ssl=1\" alt=\"Migrate Users Table in Codeigniter 4\" class=\"wp-image-5986\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-09-04-28.png?w=904&amp;ssl=1 904w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-09-04-28.png?resize=300%2C43&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-09-04-28.png?resize=768%2C110&amp;ssl=1 768w\" sizes=\"auto, (max-width: 904px) 100vw, 904px\" \/><figcaption><strong>Migrate Users Table in Codeigniter 4<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>On the migration, the specified tables will be created into the database.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"438\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-21-53-16.png?resize=1024%2C438&#038;ssl=1\" alt=\"Users Table Schema in Database\" class=\"wp-image-5987\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-21-53-16.png?resize=1024%2C438&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-21-53-16.png?resize=300%2C128&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-21-53-16.png?resize=768%2C328&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-21-53-16.png?w=1036&amp;ssl=1 1036w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Users Table Schema in Database<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/how-to-create-github-login-in-laravel-8-using-socialite\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Create Github Login in Laravel 8 Using Socialite<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Model_in_Codeigniter_4\"><\/span><strong>Create Model in Codeigniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In Codeigniter 4, you can create a model using spark. So, according to our project, we will create one model for the user. Actually, we already created the migration for the users table. Hence, open the terminal and create a new model file.<\/p>\n\n\n\n<pre title=\"create model in codeigniter 4\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php spark make:model User<\/code><\/pre>\n\n\n\n<p>The above command will create a model for the user.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"984\" height=\"98\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-21-59-43.png?resize=984%2C98&#038;ssl=1\" alt=\"Model created using spark\" class=\"wp-image-5989\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-21-59-43.png?w=984&amp;ssl=1 984w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-21-59-43.png?resize=300%2C30&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-12-21-59-43.png?resize=768%2C76&amp;ssl=1 768w\" sizes=\"auto, (max-width: 984px) 100vw, 984px\" \/><figcaption><strong>Model Created Using Spark<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Once you created the model, make it look like this.<\/p>\n\n\n\n<pre title=\"User.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nnamespace App\\Models;\n\nuse CodeIgniter\\Model;\n\nclass User extends Model\n{\n    protected $DBGroup              = 'default';\n    protected $table                = 'users';\n    protected $primaryKey           = 'id';\n    protected $useAutoIncrement     = true;\n    protected $protectFields        = true;\n    protected $allowedFields        = ['name', 'email', 'password', 'phone', 'address'];\n\n    \/\/ Dates\n    protected $useTimestamps        = false;\n    protected $dateFormat           = 'datetime';\n    protected $createdField         = 'created_at';\n    protected $updatedField         = 'updated_at';\n\n}\n<\/code><\/pre>\n\n\n\n<p>After filling up the allowed fields in the User model, we will move to the next step.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/create-login-with-twitter-in-laravel-8-using-socialite\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Create Login with Twitter in Laravel 8 Using Socialite<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_a_Controller_For_Form_Validation_in_Codeigniter_4\"><\/span><strong>Create a Controller For Form Validation in Codeigniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For getting the inputs from the form, we required a controller. After reading the form data, we will set the validation rules on those inputs. In Codeigniter 4, you can create a controller using the spark command.<\/p>\n\n\n\n<pre title=\"create controller\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php spark make:controller UserController<\/code><\/pre>\n\n\n\n<p>Here, the <strong>UserController<\/strong> has been created. Now, let&#8217;s put the functionality on it.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"744\" height=\"122\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-08-04-54.png?resize=744%2C122&#038;ssl=1\" alt=\"create a controller for form validation in codeigniter 4\" class=\"wp-image-6007\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-08-04-54.png?w=744&amp;ssl=1 744w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-08-04-54.png?resize=300%2C49&amp;ssl=1 300w\" sizes=\"auto, (max-width: 744px) 100vw, 744px\" \/><figcaption><strong>Create a Controller For Form Validation in Codeigniter 4<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<pre title=\"UserController.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nnamespace App\\Controllers;\n\nuse App\\Controllers\\BaseController;\n\nclass UserController extends BaseController\n{\n    \/**\n     * constructor\n     *\/\n    public function __construct()\n    {\n        helper(['form', 'url']);\n    }\n\n    \/**\n     * User Registration form\n     *\n     * @return void\n     *\/\n    public function index()\n    {\n        return view('registration');\n    }\n\n    \/**\n     * Register User\n     *\n     * @return void\n     *\/\n    public function create() {\n        $inputs = $this-&gt;validate([\n            'name' =&gt; 'required|min_length[5]',\n            'email' =&gt; 'required|valid_email',\n            'password' =&gt; 'required|min_length[5]|alpha_numeric',\n            'confirm_password' =&gt; 'required|matches[password]',\n            'phone' =&gt; 'required|numeric|regex_match[\/^[0-9]{10}$\/]',\n            'address' =&gt; 'required|min_length[10]'\n        ]);\n\n        if (!$inputs) {\n            return view('registration', [\n                'validation' =&gt; $this-&gt;validator\n            ]);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>The above functions will require the routes. So, in the next step, we will be creating the routes.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/create-linkedin-login-in-laravel-8-using-socialite\/\" target=\"_blank\" rel=\"noreferrer noopener\">Create LinkedIn Login in Laravel 8 Using Socialite<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Routes_For_Form_Validation_in_Codeigniter_4\"><\/span><strong>Create Routes For Form Validation in Codeigniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For creating the routes, just open the <strong>app\/Config\/Routes.php<\/strong> file. Now, put the below routes there.<\/p>\n\n\n\n<pre title=\"Routes.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$routes-&gt;get('user', 'UserController::index');\n$routes-&gt;post('user', 'UserController::create');<\/code><\/pre>\n\n\n\n<p>After adding the routes, we will create a view for the User Registration.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/create-login-with-google-using-socialite-in-laravel-8\/\" target=\"_blank\" rel=\"noreferrer noopener\">Create Socialite Login with Google Account in Laravel 8<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_a_View_For_Form_Handling\"><\/span><strong>Create a View For Form Handling<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You have to create a form for the validation of inputs. Hence, for creating a form, you need a view that will contain the form. Hence, create a view inside the <strong>app\/Views<\/strong> folder. I am going to create a view with the name <strong>registration.php<\/strong>.<\/p>\n\n\n\n<pre title=\"registration.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;!doctype html&gt;\n&lt;html lang=\"en\"&gt;\n  &lt;head&gt;\n    &lt;title&gt;Codeigniter 4 Form Validation Example&lt;\/title&gt;\n    &lt;meta charset=\"utf-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\"&gt;\n\n    &lt;!-- bootstrap 5 CSS --&gt;\n    &lt;link href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.0.0-beta3\/dist\/css\/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-eOJMYsd53ii+scO\/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6\" crossorigin=\"anonymous\"&gt;\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap-icons@1.4.1\/font\/bootstrap-icons.css\"&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n      &lt;div class=\"container py-4\"&gt;\n      &lt;?php $validation =  \\Config\\Services::validation(); ?&gt;\n        &lt;div class=\"row\"&gt;\n            &lt;div class=\"col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12 m-auto\"&gt;\n                &lt;form method=\"POST\" action=\"&lt;?= base_url('user') ?&gt;\"&gt;\n                    &lt;?= csrf_field() ?&gt;\n\n                    &lt;div class=\"card shadow\"&gt;\n                        &lt;div class=\"card-header\"&gt;\n                            &lt;h5 class=\"card-title\"&gt;Register User&lt;\/h5&gt;\n                        &lt;\/div&gt;\n\n                        &lt;div class=\"card-body p-4\"&gt;\n                        \n                            &lt;div class=\"form-group mb-3 has-validation\"&gt;\n                                &lt;label class=\"form-label\"&gt;Name&lt;\/label&gt;\n                                &lt;input type=\"text\" class=\"form-control &lt;?php if($validation-&gt;getError('name')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"name\" placeholder=\"Name\" value=\"&lt;?php echo set_value('name'); ?&gt;\"\/&gt;\n                                &lt;?php if ($validation-&gt;getError('name')): ?&gt;\n                                    &lt;div class=\"invalid-feedback\"&gt;\n                                        &lt;?= $validation-&gt;getError('name') ?&gt;\n                                    &lt;\/div&gt;                                \n                                &lt;?php endif; ?&gt;\n                            &lt;\/div&gt;\n\n                            &lt;div class=\"form-group mb-3\"&gt;\n                                &lt;label class=\"form-label\"&gt;Email&lt;\/label&gt;\n                                &lt;input type=\"text\" class=\"form-control &lt;?php if($validation-&gt;getError('email')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"email\" placeholder=\"Email\" value=\"&lt;?php echo set_value('email'); ?&gt;\"\/&gt;\n                                    &lt;?php if ($validation-&gt;getError('email')): ?&gt;\n                                    &lt;div class=\"invalid-feedback\"&gt;\n                                        &lt;?= $validation-&gt;getError('email') ?&gt;\n                                    &lt;\/div&gt;                                \n                                &lt;?php endif; ?&gt;\n                            &lt;\/div&gt;\n\n                            &lt;div class=\"form-group mb-3\"&gt;\n                                &lt;label class=\"form-label\"&gt;Password&lt;\/label&gt;\n                                &lt;input type=\"password\" class=\"form-control &lt;?php if($validation-&gt;getError('password')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"password\" placeholder=\"Password\" value=\"&lt;?php echo set_value('password'); ?&gt;\"\/&gt;\n                                    &lt;?php if ($validation-&gt;getError('password')): ?&gt;\n                                    &lt;div class=\"invalid-feedback\"&gt;\n                                        &lt;?= $validation-&gt;getError('password') ?&gt;\n                                    &lt;\/div&gt;                                \n                                &lt;?php endif; ?&gt;\n                            &lt;\/div&gt;\n\n                            &lt;div class=\"form-group mb-3\"&gt;\n                                &lt;label class=\"form-label\"&gt;Confirm Password&lt;\/label&gt;\n                                &lt;input type=\"password\" class=\"form-control &lt;?php if($validation-&gt;getError('confirm_password')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"confirm_password\" placeholder=\"Confirm Password\" value=\"&lt;?php echo set_value('confirm_password'); ?&gt;\"\/&gt;\n                                    &lt;?php if ($validation-&gt;getError('confirm_password')): ?&gt;\n                                    &lt;div class=\"invalid-feedback\"&gt;\n                                        &lt;?= $validation-&gt;getError('confirm_password') ?&gt;\n                                    &lt;\/div&gt;                                \n                                &lt;?php endif; ?&gt;\n                            &lt;\/div&gt;\n\n                            &lt;div class=\"form-group mb-3\"&gt;\n                                &lt;label class=\"form-label\"&gt;Phone&lt;\/label&gt;\n                                &lt;input type=\"text\" class=\"form-control &lt;?php if($validation-&gt;getError('phone')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"phone\" placeholder=\"Phone\" value=\"&lt;?php echo set_value('phone'); ?&gt;\"\/&gt;\n                                    &lt;?php if ($validation-&gt;getError('phone')): ?&gt;\n                                    &lt;div class=\"invalid-feedback\"&gt;\n                                        &lt;?= $validation-&gt;getError('phone') ?&gt;\n                                    &lt;\/div&gt;                                \n                                &lt;?php endif; ?&gt;\n                            &lt;\/div&gt;\n\n                            &lt;div class=\"form-group\"&gt;\n                                &lt;label class=\"form-label\"&gt;Address&lt;\/label&gt;\n                                &lt;textarea class=\"form-control &lt;?php if($validation-&gt;getError('address')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"address\" placeholder=\"Address\"&gt;&lt;?php echo set_value('address'); ?&gt;&lt;\/textarea&gt; \n                                    &lt;?php if ($validation-&gt;getError('address')): ?&gt;\n                                    &lt;div class=\"invalid-feedback\"&gt;\n                                        &lt;?= $validation-&gt;getError('address') ?&gt;\n                                    &lt;\/div&gt;                                \n                                &lt;?php endif; ?&gt;\n                            &lt;\/div&gt;\n\n                        &lt;\/div&gt;\n\n                        &lt;div class=\"card-footer\"&gt;\n                            &lt;button type=\"submit\" class=\"btn btn-success\"&gt;Save&lt;\/button&gt;\n                        &lt;\/div&gt;\n                    &lt;\/div&gt;\n                &lt;\/form&gt;\n            &lt;\/div&gt;\n        &lt;\/div&gt;\n      &lt;\/div&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>Save and run the application to see the result.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/how-to-create-facebook-login-in-laravel-8-using-socialite\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Create Facebook Login in Laravel 8 Using Socialite<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Check_the_Validation_Result\"><\/span><strong>Check the Validation Result<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Open the favorite browser and run the Codeigniter project. Go to the <code>http:\/\/localhost:8080\/user<\/code>. You will see a registration form as showing below.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"961\" height=\"698\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-17-47-40.png?resize=961%2C698&#038;ssl=1\" alt=\"User Registration Form\" class=\"wp-image-6015\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-17-47-40.png?w=961&amp;ssl=1 961w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-17-47-40.png?resize=300%2C218&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-17-47-40.png?resize=768%2C558&amp;ssl=1 768w\" sizes=\"auto, (max-width: 961px) 100vw, 961px\" \/><figcaption><strong>User Registration Form<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>We have already set the validation rules for this form. Now, let&#8217;s try submitting the form. As per the validation rules, you will be getting the error message in the form.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"644\" height=\"691\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-17-57-07.png?resize=644%2C691&#038;ssl=1\" alt=\"Form Validation Result\" class=\"wp-image-6019\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-17-57-07.png?w=644&amp;ssl=1 644w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-17-57-07.png?resize=280%2C300&amp;ssl=1 280w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><figcaption><strong>Form Validation Result in Codeigniter 4<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>In the above validation error message, you can notice one thing. The validation error message is throwing on the basis of the input name. Suppose, for the Password Confirmation, the input name is <strong>confirm_password<\/strong>. Hence, the validation error message for this has thrown on the basis of the same name. So, what if we want to change this label of validation error message.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/task-scheduling-in-laravel-8-schedule-tasks-using-cron-job\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Schedule Tasks with Cron Job in Laravel 8<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Change_Label_For_Validation_Error_Message_in_Codeigniter_4\"><\/span><strong>Change Label For Validation Error Message in Codeigniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We can change the label of every inputs for displaying the error message. It will make more meaningful to our form validation. So, you have to add the attribute in the form of array as showing below.<\/p>\n\n\n\n<pre title=\"validation rules\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$inputs = $this-&gt;validate([\n            'name' =&gt; ['label' =&gt; 'Name', 'rules' =&gt; 'required|min_length[5]'],\n            'email' =&gt; ['label' =&gt; 'Email', 'rules' =&gt; 'required|valid_email'],\n            'password' =&gt; ['label' =&gt; 'Password', 'rules' =&gt; 'required|min_length[5]|alpha_numeric'],\n            'confirm_password' =&gt; ['label' =&gt; 'Confirm Password', 'rules' =&gt; 'required|matches[password]'],\n            'phone' =&gt; ['label' =&gt; 'Mobile number', 'rules' =&gt; 'required|numeric|regex_match[\/^[0-9]{10}$\/]'],\n            'address' =&gt; ['label' =&gt; 'Address', 'rules' =&gt; 'required|min_length[10]']\n        ]);<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"655\" height=\"676\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-20-48-54.png?resize=655%2C676&#038;ssl=1\" alt=\"Form Validation Error Message After Changing Label\" class=\"wp-image-6025\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-20-48-54.png?w=655&amp;ssl=1 655w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-20-48-54.png?resize=291%2C300&amp;ssl=1 291w\" sizes=\"auto, (max-width: 655px) 100vw, 655px\" \/><figcaption><strong>Form Validation Error Message After Changing Label<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/import-and-export-csv-and-excel-file-in-laravel-8\/\" target=\"_blank\" rel=\"noreferrer noopener\">Import and Export CSV and Excel File in Laravel 8<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Set_Custom_Validation_Error_Message\"><\/span><strong>Set Custom Validation Error Message <\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In the Codeigniter form validation, you can set the custom validation error message. It is really very simple to has a custom message for every rule. For creating the custom validation error message, you can put the <strong>errors<\/strong> attribute ins the form of an array.<\/p>\n\n\n\n<p>So, let&#8217;s see how you can achieve this.<\/p>\n\n\n\n<pre title=\"custom validation error message\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">       $inputs = $this-&gt;validate([\n            'name' =&gt; [\n                'label' =&gt; 'Name',\n                'rules' =&gt; 'required|min_length[5]',\n                'errors' =&gt; [\n                    'required' =&gt; 'Please enter your name.',\n                    'min_length' =&gt; 'Name must be atleast 5 characters long.'\n                ]\n            ],\n            'email' =&gt; [\n                'label' =&gt; 'Email',\n                'rules' =&gt; 'required|valid_email',\n                'errors' =&gt; [\n                    'required' =&gt; 'Enter your email.',\n                    'valid_email' =&gt; 'Please enter a valid email address.'\n                ]\n            ],\n            'password' =&gt; [\n                'label' =&gt; 'Password',\n                'rules' =&gt; 'required|min_length[5]|alpha_numeric',\n                'errors' =&gt; [\n                    'required' =&gt; 'Enter your password.',\n                    'min_length' =&gt; 'Password must be atleast 5 digits.',\n                    'alpha_numeric' =&gt; 'Password must contain alpha numeric'\n                ]\n            ],\n            'confirm_password' =&gt; [\n                'label' =&gt; 'Confirm Password',\n                'rules' =&gt; 'required|matches[password]',\n                'errors' =&gt; [\n                    'required' =&gt; 'Re-enter your password.',\n                    'matches' =&gt; 'Confirm password and password must be same.'\n                ],\n            ],\n            'phone' =&gt; [\n                'label' =&gt; 'Mobile number',\n                'rules' =&gt; 'required|numeric|regex_match[\/^[0-9]{10}$\/]',\n                'errors' =&gt; [\n                    'required' =&gt; 'Enter your mobile number.',\n                    'numeric' =&gt; 'Mobile number must be a number.',\n                    'regex_match' =&gt; 'Mobile number must be a valid mobile number.'\n                ]\n            ],\n            'address' =&gt; [\n                'label' =&gt; 'Address',\n                'rules' =&gt; 'required|min_length[10]',\n                'errors' =&gt; [\n                    'required' =&gt; 'Enter your address.',\n                    'min_length' =&gt; 'Address must be atleast 10 characters long.'\n                ]\n            ]\n        ]);<\/code><\/pre>\n\n\n\n<p>Save and run the application. Now, re-check the validation errors by submitting the form.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"649\" height=\"672\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-21-42-11.png?resize=649%2C672&#038;ssl=1\" alt=\"Custom Validation Error Message\" class=\"wp-image-6030\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-21-42-11.png?w=649&amp;ssl=1 649w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-21-42-11.png?resize=290%2C300&amp;ssl=1 290w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><figcaption><strong>Custom Validation Error Message<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Also, you can see the custom validation error message for every specific rule.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"649\" height=\"662\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-21-43-20.png?resize=649%2C662&#038;ssl=1\" alt=\"Custom Validation Error Message For Specific Rule\" class=\"wp-image-6031\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-21-43-20.png?w=649&amp;ssl=1 649w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-13-21-43-20.png?resize=294%2C300&amp;ssl=1 294w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><figcaption><strong>Custom Validation Error Message For Specific Rule<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>After the validation, you can insert the data through the model. So, let&#8217;s add few lines of code after the validation rules.<\/p>\n\n\n\n<pre title=\"insert data through the model - UserController.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$user = new User;\n$user-&gt;save([\n    'name' =&gt; $this-&gt;request-&gt;getVar('name'),\n    'email'  =&gt; $this-&gt;request-&gt;getVar('email'),\n    'password'  =&gt; $this-&gt;request-&gt;getVar('email'),\n    'phone'  =&gt; $this-&gt;request-&gt;getVar('phone'),\n    'address'  =&gt; $this-&gt;request-&gt;getVar('address')\n]);\n\nsession()-&gt;setFlashdata('success', 'Success! registration completed.');\nreturn redirect()-&gt;to(site_url('\/user'));<\/code><\/pre>\n\n\n\n<p>In the above snippet, I have created an object of the User model class. Then using the <strong>save()<\/strong> method, I have inserted the data in the form of array. <\/p>\n\n\n\n<p>We have to display the flash data message to the view. So, just add few lines in the <strong>Views\/registration.php<\/strong>.<\/p>\n\n\n\n<pre title=\"display flash data message\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;!-- display flash data message --&gt;\n&lt;?php\n    if(session()-&gt;getFlashdata('success')):?&gt;\n        &lt;div class=\"alert alert-success alert-dismissible\"&gt;\n            &lt;button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\"&gt;&amp;times;&lt;\/button&gt;\n            &lt;?php echo session()-&gt;getFlashdata('success') ?&gt;\n        &lt;\/div&gt;\n    &lt;?php elseif(session()-&gt;getFlashdata('failed')):?&gt;\n        &lt;div class=\"alert alert-danger alert-dismissible\"&gt;\n            &lt;button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\"&gt;&amp;times;&lt;\/button&gt;\n            &lt;?php echo session()-&gt;getFlashdata('failed') ?&gt;\n        &lt;\/div&gt;\n&lt;?php endif; ?&gt;<\/code><\/pre>\n\n\n\n<p>Now, you will have the result of success after inserting the data.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"648\" height=\"676\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-14-06-49-12.png?resize=648%2C676&#038;ssl=1\" alt=\"Flash Data Message After User Registration\" class=\"wp-image-6038\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-14-06-49-12.png?w=648&amp;ssl=1 648w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/Screenshot-from-2021-05-14-06-49-12.png?resize=288%2C300&amp;ssl=1 288w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><figcaption><strong>Flash Data Message After User Registration<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>So, the final snippet of the <strong>UserController.php<\/strong> will be as showing below.<\/p>\n\n\n\n<pre title=\"UserController.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nnamespace App\\Controllers;\n\nuse App\\Controllers\\BaseController;\nuse App\\Models\\User;\n\nclass UserController extends BaseController\n{\n    \/**\n     * constructor\n     *\/\n    public function __construct()\n    {\n        helper(['form', 'url']);\n    }\n\n    \/**\n     * User Registration form\n     *\n     * @return void\n     *\/\n    public function index()\n    {\n        return view('registration');\n    }\n\n    \/**\n     * Register User\n     *\n     * @return void\n     *\/\n    public function create() {\n        \n        \/\/ validate inputs\n        $inputs = $this-&gt;validate([\n            'name' =&gt; [\n                'label' =&gt; 'Name',\n                'rules' =&gt; 'required|min_length[5]',\n                'errors' =&gt; [\n                    'required' =&gt; 'Please enter your name.',\n                    'min_length' =&gt; 'Name must be atleast 5 characters long.'\n                ]\n            ],\n            'email' =&gt; [\n                'label' =&gt; 'Email',\n                'rules' =&gt; 'required|valid_email',\n                'errors' =&gt; [\n                    'required' =&gt; 'Enter your email.',\n                    'valid_email' =&gt; 'Please enter a valid email address.'\n                ]\n            ],\n            'password' =&gt; [\n                'label' =&gt; 'Password',\n                'rules' =&gt; 'required|min_length[5]|alpha_numeric',\n                'errors' =&gt; [\n                    'required' =&gt; 'Enter your password.',\n                    'min_length' =&gt; 'Password must be atleast 5 digits.',\n                    'alpha_numeric' =&gt; 'Password must contain alpha numeric'\n                ]\n            ],\n            'confirm_password' =&gt; [\n                'label' =&gt; 'Confirm Password',\n                'rules' =&gt; 'required|matches[password]',\n                'errors' =&gt; [\n                    'required' =&gt; 'Re-enter your password.',\n                    'matches' =&gt; 'Confirm password and password must be same.'\n                ],\n            ],\n            'phone' =&gt; [\n                'label' =&gt; 'Mobile number',\n                'rules' =&gt; 'required|numeric|regex_match[\/^[0-9]{10}$\/]',\n                'errors' =&gt; [\n                    'required' =&gt; 'Enter your mobile number.',\n                    'numeric' =&gt; 'Mobile number must be a number.',\n                    'regex_match' =&gt; 'Mobile number must be a valid mobile number.'\n                ]\n            ],\n            'address' =&gt; [\n                'label' =&gt; 'Address',\n                'rules' =&gt; 'required|min_length[10]',\n                'errors' =&gt; [\n                    'required' =&gt; 'Enter your address.',\n                    'min_length' =&gt; 'Address must be atleast 10 characters long.'\n                ]\n            ]\n        ]);\n\n        if (!$inputs) {\n            return view('registration', [\n                'validation' =&gt; $this-&gt;validator\n            ]);\n        }\n\n        \/\/ insert data \n        $user = new User;\n        $user-&gt;save([\n            'name' =&gt; $this-&gt;request-&gt;getVar('name'),\n            'email'  =&gt; $this-&gt;request-&gt;getVar('email'),\n            'password'  =&gt; password_hash($this-&gt;request-&gt;getVar('password'), PASSWORD_DEFAULT),\n            'phone'  =&gt; $this-&gt;request-&gt;getVar('phone'),\n            'address'  =&gt; $this-&gt;request-&gt;getVar('address')\n        ]);\n\n        session()-&gt;setFlashdata('success', 'Success! registration completed.');\n        return redirect()-&gt;to(site_url('\/user'));\n    }\n}<\/code><\/pre>\n\n\n\n<p>Now, after adding the flash data message in the <strong>registration.php<\/strong> (view), it will look like this.<\/p>\n\n\n\n<pre title=\"registration.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;!doctype html&gt;\n&lt;html lang=\"en\"&gt;\n  &lt;head&gt;\n    &lt;title&gt;Codeigniter 4 Form Validation Example&lt;\/title&gt;\n    &lt;meta charset=\"utf-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\"&gt;\n\n    &lt;!-- bootstrap 5 CSS --&gt;\n    &lt;link href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.0.0-beta3\/dist\/css\/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-eOJMYsd53ii+scO\/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6\" crossorigin=\"anonymous\"&gt;\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap-icons@1.4.1\/font\/bootstrap-icons.css\"&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n      &lt;div class=\"container py-4\"&gt;\n      &lt;?php $validation =  \\Config\\Services::validation(); ?&gt;\n        &lt;div class=\"row\"&gt;\n            &lt;div class=\"col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12 m-auto\"&gt;\n                &lt;form method=\"POST\" action=\"&lt;?= base_url('user') ?&gt;\"&gt;\n                    &lt;?= csrf_field() ?&gt;\n\n                    &lt;!-- display flash data message --&gt;\n                    &lt;?php\n                        if(session()-&gt;getFlashdata('success')):?&gt;\n                            &lt;div class=\"alert alert-success alert-dismissible\"&gt;\n                                &lt;button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\"&gt;&amp;times;&lt;\/button&gt;\n                                &lt;?php echo session()-&gt;getFlashdata('success') ?&gt;\n                            &lt;\/div&gt;\n                        &lt;?php elseif(session()-&gt;getFlashdata('failed')):?&gt;\n                            &lt;div class=\"alert alert-danger alert-dismissible\"&gt;\n                                &lt;button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\"&gt;&amp;times;&lt;\/button&gt;\n                                &lt;?php echo session()-&gt;getFlashdata('failed') ?&gt;\n                            &lt;\/div&gt;\n                    &lt;?php endif; ?&gt;\n\n                    &lt;div class=\"card shadow\"&gt;\n                        &lt;div class=\"card-header\"&gt;\n                            &lt;h5 class=\"card-title\"&gt;Register User&lt;\/h5&gt;\n                        &lt;\/div&gt;\n\n                        &lt;div class=\"card-body p-4\"&gt;\n                        \n                            &lt;div class=\"form-group mb-3 has-validation\"&gt;\n                                &lt;label class=\"form-label\"&gt;Name&lt;\/label&gt;\n                                &lt;input type=\"text\" class=\"form-control &lt;?php if($validation-&gt;getError('name')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"name\" placeholder=\"Name\" value=\"&lt;?php echo set_value('name'); ?&gt;\"\/&gt;\n                                &lt;?php if ($validation-&gt;getError('name')): ?&gt;\n                                    &lt;div class=\"invalid-feedback\"&gt;\n                                        &lt;?= $validation-&gt;getError('name') ?&gt;\n                                    &lt;\/div&gt;                                \n                                &lt;?php endif; ?&gt;\n                            &lt;\/div&gt;\n\n                            &lt;div class=\"form-group mb-3\"&gt;\n                                &lt;label class=\"form-label\"&gt;Email&lt;\/label&gt;\n                                &lt;input type=\"text\" class=\"form-control &lt;?php if($validation-&gt;getError('email')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"email\" placeholder=\"Email\" value=\"&lt;?php echo set_value('email'); ?&gt;\"\/&gt;\n                                    &lt;?php if ($validation-&gt;getError('email')): ?&gt;\n                                    &lt;div class=\"invalid-feedback\"&gt;\n                                        &lt;?= $validation-&gt;getError('email') ?&gt;\n                                    &lt;\/div&gt;                                \n                                &lt;?php endif; ?&gt;\n                            &lt;\/div&gt;\n\n                            &lt;div class=\"form-group mb-3\"&gt;\n                                &lt;label class=\"form-label\"&gt;Password&lt;\/label&gt;\n                                &lt;input type=\"password\" class=\"form-control &lt;?php if($validation-&gt;getError('password')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"password\" placeholder=\"Password\" value=\"&lt;?php echo set_value('password'); ?&gt;\"\/&gt;\n                                    &lt;?php if ($validation-&gt;getError('password')): ?&gt;\n                                    &lt;div class=\"invalid-feedback\"&gt;\n                                        &lt;?= $validation-&gt;getError('password') ?&gt;\n                                    &lt;\/div&gt;                                \n                                &lt;?php endif; ?&gt;\n                            &lt;\/div&gt;\n\n                            &lt;div class=\"form-group mb-3\"&gt;\n                                &lt;label class=\"form-label\"&gt;Confirm Password&lt;\/label&gt;\n                                &lt;input type=\"password\" class=\"form-control &lt;?php if($validation-&gt;getError('confirm_password')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"confirm_password\" placeholder=\"Confirm Password\" value=\"&lt;?php echo set_value('confirm_password'); ?&gt;\"\/&gt;\n                                    &lt;?php if ($validation-&gt;getError('confirm_password')): ?&gt;\n                                    &lt;div class=\"invalid-feedback\"&gt;\n                                        &lt;?= $validation-&gt;getError('confirm_password') ?&gt;\n                                    &lt;\/div&gt;                                \n                                &lt;?php endif; ?&gt;\n                            &lt;\/div&gt;\n\n                            &lt;div class=\"form-group mb-3\"&gt;\n                                &lt;label class=\"form-label\"&gt;Phone&lt;\/label&gt;\n                                &lt;input type=\"text\" class=\"form-control &lt;?php if($validation-&gt;getError('phone')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"phone\" placeholder=\"Phone\" value=\"&lt;?php echo set_value('phone'); ?&gt;\"\/&gt;\n                                    &lt;?php if ($validation-&gt;getError('phone')): ?&gt;\n                                    &lt;div class=\"invalid-feedback\"&gt;\n                                        &lt;?= $validation-&gt;getError('phone') ?&gt;\n                                    &lt;\/div&gt;                                \n                                &lt;?php endif; ?&gt;\n                            &lt;\/div&gt;\n\n                            &lt;div class=\"form-group\"&gt;\n                                &lt;label class=\"form-label\"&gt;Address&lt;\/label&gt;\n                                &lt;textarea class=\"form-control &lt;?php if($validation-&gt;getError('address')): ?&gt;is-invalid&lt;?php endif ?&gt;\" name=\"address\" placeholder=\"Address\"&gt;&lt;?php echo set_value('address'); ?&gt;&lt;\/textarea&gt; \n                                    &lt;?php if ($validation-&gt;getError('address')): ?&gt;\n                                    &lt;div class=\"invalid-feedback\"&gt;\n                                        &lt;?= $validation-&gt;getError('address') ?&gt;\n                                    &lt;\/div&gt;                                \n                                &lt;?php endif; ?&gt;\n                            &lt;\/div&gt;\n\n                        &lt;\/div&gt;\n\n                        &lt;div class=\"card-footer\"&gt;\n                            &lt;button type=\"submit\" class=\"btn btn-success\"&gt;Save&lt;\/button&gt;\n                        &lt;\/div&gt;\n                    &lt;\/div&gt;\n                &lt;\/form&gt;\n            &lt;\/div&gt;\n        &lt;\/div&gt;\n      &lt;\/div&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>That&#8217;s it for the form validation in Codeigniter 4.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this Codeigniter 4 tutorial, I have shown you the basic idea of validating the form. You can create a large number of inputs in any form and set the validation rules on it. I also, shown you to set the custom error message for every specific validation rule. You can use regular expression to validation on any specific patter. As, we did for the mobile number validation. Therefore, I hope, you can take a quick guide for Codeigniter 4 form validation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Codeigniter provides the form helper to validate the form. You can easily validate the inputs like name, email, password, and many more fields. In the entire form validation process, the form will submit to the specified action. While submitting the form to the action, it will check the form inputs as per the specified rules. [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":6033,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1977],"tags":[2066,2068,454,2069],"yst_prominent_words":[134,442],"class_list":{"0":"post-5971","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-codeigniter-4","8":"tag-codeigniter-4-form-validation","9":"tag-custom-validation-error","10":"tag-form-validation","11":"tag-regex-form-validation-in-codeigniter","12":"entry"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/05\/form-validation.png?fit=2240%2C1260&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/5971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/comments?post=5971"}],"version-history":[{"count":1,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/5971\/revisions"}],"predecessor-version":[{"id":8647,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/5971\/revisions\/8647"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media\/6033"}],"wp:attachment":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media?parent=5971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/categories?post=5971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/tags?post=5971"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/yst_prominent_words?post=5971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}