Search code examples
phpcodeignitercodeigniter-form-validation

Codeigniter 2.0.2 form validation set_rules custom error message not working


I'm trying to add a custom error message on for codeigniter form validation.

$this->load->library('form_validation');
$this->form_validation->set_rules('month', 'Month', 'trim|required', array('required' => 'You need to supply period starting month'));

But unfortunately I'm still getting

The Month field is required.

error message.

My ci version is 2.0.2


Solution

  • In CI 2, you need to set the custom error messages like below:

    $this->form_validation->set_message('rule', 'Error Message');
    

    And as your requirement this may work,

    $this->form_validation->set_rules('month', 'period starting month', 'trim|required');
    
    $this->form_validation->set_message('required', 'You need to supply %s');
    

    Please see the Docs

    set_rules does not set the validation messages in the version you are using.