Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom form validation error message for Codeigniter 2

I have a drop down named "business_id".

<select name="business_id"> 
    <option value="0">Select Business</option> More options... 
</select>

Here comes the validation rule, user must select an option.

$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]');

Problem being the error message says: The Business field must contain a number greater than 0. Not very intuitive! I want it to say "You must select a business".

I tried:

$this->form_validation->set_message('Business', 'You must select a business');

But CI complete ignores this. Does anyone have a solution for this?

like image 389
pigfox Avatar asked Apr 30 '11 03:04

pigfox


People also ask

How to set custom error message in CodeIgniter form validation?

Show activity on this post. if(YOUR_CONDITION){ $this->form_validation->run(); $err = validation_errors(); $err = $err. '<p>Custom validation error message</p>'. PHP_EOL; $data['err'] = $err; $this->load->view('viewname', $data); } else if ($this->form_validation->run() == true ) { #code... } else..

How to give form validation in CodeIgniter?

Create a view file myform. php and save the below code it in application/views/myform. php. This page will display form where user can submit his name and we will validate this page to ensure that it should not be empty while submitting.


2 Answers

I had the same requirement for adding custom form validation error messages in codeigniter 2 (e.g. "You must agree to our Terms & Conditions"). Naturally it would be wrong to override the error messages for require and greater_than as it would erroneously produce messages for the rest of the form. I extended the CI_Form_validation class and have overridden the set_rules method to accept a new 'message' parameter:

<?php

class MY_Form_validation extends CI_Form_validation
{
    private $_custom_field_errors = array();

    public function _execute($row, $rules, $postdata = NULL, $cycles = 0)
    {
        // Execute the parent method from CI_Form_validation.
        parent::_execute($row, $rules, $postdata, $cycles);

        // Override any error messages for the current field.
        if (isset($this->_error_array[$row['field']])
            && isset($this->_custom_field_errors[$row['field']]))
        {
            $message = str_replace(
                '%s',
                !empty($row['label']) ? $row['label'] : $row['field'],
                $this->_custom_field_errors[$row['field']]);

            $this->_error_array[$row['field']] = $message;
            $this->_field_data[$row['field']]['error'] = $message;
        }
    }

    public function set_rules($field, $label = '', $rules = '', $message = '')
    {
        $rules = parent::set_rules($field, $label, $rules);

        if (!empty($message))
        {
            $this->_custom_field_errors[$field] = $message;
        }

        return $rules;
    }
}

?>

With the above class you would produce your rule with a custom error message like so:

$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]', 'You must select a business');

You may also use '%s' in your custom message which will automatically fill in the label of fieldname.

like image 142
Andrew Mackrodt Avatar answered Sep 30 '22 05:09

Andrew Mackrodt


If you'd like to customize the error messages that are displayed with each rule, you can find them in an array at:

/system/language/english/form_validation_lang.php
like image 27
misterchristos Avatar answered Sep 30 '22 05:09

misterchristos