Alatha Ntonga
Alatha Ntonga

Reputation: 73

How do I Set custom error messages on Codeigniter

I'm trying to set a custom error message for a particular field on some particular rule as per codeigniter's doc, and I'm running into issues. My code looks like the following

function start(){
$this->form_validation->set_message('rule1', 'Rule 1 Error Message');

$this->form_validation->set_rules('amount', lang('users amount'), 'required|trim|numeric|greater_than[0]', array('rule1' => 'Error Message on rule1 for this field_name'));

if ($this->form_validation->run() == FALSE)
{
  $this->index();
}
else{
$amount =$this->input->post("amount", TRUE);
}

I've tested the rules for each field and they work fine, the problem starts when I add a custom error message for each form field. Previously, if there was an error I'd redirect and display a generic message for the entire form.

$this->session->set_flashdata('error', "Please provide correct data about the transfer");

With the custom message it doesn't throw the custom message at all when I enter data that doesn't obey the rules, however it does throw an error for a pre-defined rules like min_length or max length.

I've followed the docs to a tee, and I don't seee why this issues is happening. I'm aware that there is a way to throw custom messages using callback functions, but preferably I'd like to use this method.

Upvotes: 0

Views: 497

Answers (1)

butterfly27
butterfly27

Reputation: 38

Please try the following steps:

$this->session->set_flashdata('field_name', "Any custom error message");
redirect('page_URL');//use url to be redirected upon.

OR

$this->form_validation->set_message('is_unique', 'The %s is already taken');

The form_validation changes %s with the label seted for the field.

I hope one of these will helps!

Upvotes: 1

Related Questions