How to customize form validation errors in codeIgniter

codeigniter form validation custom error message
codeigniter form validation error message display
registration form validation in codeigniter
codeigniter form validation example
codeigniter custom validation
codeigniter form validation array fields
form validation not working in codeigniter
codeigniter date validation

Is there a file in codeIgniter in which I could just edit so that I can customize the form validation messages?

I just want to place them in a bulleted list to consume less space.

Here's the code that I'm using to output the error messages:

<div class="alert <?php echo $alert['alert_type']; ?> min-form">
        <button type="button" class="close" data-dismiss="alert">x</button>
        <h4><?php echo $alert['main_message']; ?></h4> 
        <?php echo $alert['sub_message']; ?>

Basically the $alert['sub_message'] is just getting its data from validation_errors() function from CodeIgniter which outputs the validation errors from a form.

You can extend the form_validation class for maximum control by creating application/libraries/MY_form_validation.php to add extra validation rules - I've attached an example below.

It is bad practice to edit system libraries directly; CI provides better options (overrides/customization thru MY_ classes, libraries, hooks, etc). This gives you the benefit of easily upgrading CI versions & keeps your application portable / custom code isolated from the core framework.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

 * CodeIgniter Form Validation Extension
class MY_Form_validation extends CI_Form_validation {

     *  MY_Form_validation::valid_url
     * @abstract Ensures a string is a valid URL
    function valid_url($url) {
        if(preg_match("/^http(|s):\/{2}(.*)\.([a-z]){2,}(|\/)(.*)$/i", $url)) {
            if(filter_var($url, FILTER_VALIDATE_URL)) return TRUE;
        $this->CI->form_validation->set_message('valid_url', 'The %s must be a valid URL.');
        return FALSE;

     * MY_Form_validation::alpha_extra()
     * @abstract Alpha-numeric with periods, underscores, spaces and dashes
    function alpha_extra($str) {
        $this->CI->form_validation->set_message('alpha_extra', 'The %s may only contain alpha-numeric characters, spaces, periods, underscores & dashes.');
        return ( ! preg_match("/^([\.\s-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;

     * MY_Form_validation::numeric_comma()
     * @abstract Numeric and commas characters
    function numeric_comma($str) {
        $this->CI->form_validation->set_message('numeric_comma', 'The %s may only contain numeric & comma characters.');
        return ( ! preg_match("/^(\d+,)*\d+$/", $str)) ? FALSE : TRUE;

     * MY_Form_validation::matches_pattern()
     * @abstract Ensures a string matches a basic pattern
    function matches_pattern($str, $pattern) {
        if (preg_match('/^' . $pattern . '$/', $str)) return TRUE;
        $this->CI->form_validation->set_message('matches_pattern', 'The %s field does not match the required pattern.');
        return FALSE;


/* End of file MY_form_validation.php */
/* Location: ./{APPLICATION}/libraries/MY_form_validation.php */

How to customize form validation errors in codeIgniter, 1. Changing delimiters Globally: To globally change the error delimiters, in your controller method, just after loading the Form Validation class. Load after the form_validation library load. you can load in the constructor as well. In order to implement form validation you'll need three things: A View file containing a form. A View file containing a "success" message to be displayed upon successful submission. A controller function to receive and process the submitted data.

You can change the Error Delimiters in this way:

<?php echo validation_errors('<li>', '</li>'); ?>


For v3:

Form Validation, Changing the Error Delimiters​​ Changing delimiters Globally To globally change the error delimiters, in your controller method, just after loading the Form Validation class, add this: $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); It uses a form helper to create the form opening. Technically, this isn’t necessary. You could create the form using standard HTML. However, the benefit of using the helper is that it generates the action URL for you, based on the URL in your config file. This makes your application more portable in the event your URLs change.

You can use <?php echo form_error('field name', '<div class="error">', '</div>'); ?> for displaying errors individually.


Form Validation : CodeIgniter User Guide, Form Validation class adds a paragraph tag (<p>) around each error message shown. You can either change  CodeIgniter Form Validation. Validation plays a very critical role when processing data from forms. Let's say a user is signing up on a website; we want to make sure that they fill in their required details and email address. We need to make sure that the email address that has been entered is valid.

You can change the delimiters from <div> to <li> by using CodeIgniter's set_error_delimiters function:

$this->form_validation->set_error_delimiters('<li>', '</li>');

You should do this immediately after loading the Form Validation class.

This will change the way validation_errors() and form_error('field_name') are displayed. So you will need to add ul or ol as follows:

echo '<ul>' . validation_errors() . '</ul>';

CodeIgniter Form Validation: From Start to Finish, Custom error messages; Custom validation callback; Validation configuration. Go Through Basic Validation Rules. In this section, we'll go through  You were almost there with your initial thought. All you needed was to execute validation after each group of set_message() changes. I find this approach much easier and faster than callback functions, unless you are going to use this exact customisation in many places.

The same as answers above, just if you want with bootstrap:

<?php echo validation_errors('<li><span class="label label-danger">', '</span></li>'); ?>

Form Validation with Custom Message not displaying message , If u want change any error message for any particular input element of form, then i think u need to use a callback function & define u'r error  Codeigniter provide form_validation library for set server side validation, but if we make it simple then it always refresh page, so it would be better if you use jquery ajax for form validation. So here i gave you full example of form validation in codeigniter application. i created simple form with first name, last name, email and address

Form Validation Error Messages in Codeigniter, we will use form_validation library for add form validation with error message display in codeigniter. we can use default following validation rules  One best way to achieve this is extending CodeIgniter’s Form Validation library. Let say we want to create a custom validator named access_code_unique for the field access_code of the database table users. All you have to do is creating a Class file named MY_Form_validation.php in application/libraries directory.

Learn Codeigniter Form Validation in Simple Steps, Understand the basics of Codeigniter form validation with this guide and the users with error messages and configure relevant route/library in  library(‘form_validation’) → Load validation library which is cwformvalidation. form_validation->set_rules → set validation rules. Once the user clicks the Submit button, the Controller will load the library and execute the validation rules to ensure that the information submitted by the user fit the validation rules.

Codeigniter Form Validation Tutorial Example, to add server-side validation with custom error messages and callback to the login and registration using Codeigniter form validation class. So here i gave you full example of form validation in codeigniter application. i created simple form with first name, last name, email and address like contact us form and i set server side validation.