1

I am working on already built codeigniter application. I am working on enhancements. One of the enhancement is to change the validation messages. So I checked the validation messages are drive through CI_Form_validation library in codeigniter. I want manage to set the custom messages using the "set_message".

$this->form_validation->set_message('required', 'Please enter %s.');

This is triggering for all fields where the values is empty. Which is good. But I have few select fields and radio buttons the application. For those message should change from "Please enter %s" to "Please select %s". I tried callback methods mentioned in the " How can I setup custom error messages for each form field in Codeigniter? "

And also I have tried the method mentioned in the following links

1) " https://github.com/EllisLab/CodeIgniter/wiki/Custom-Validation-Errors-per-Field ".
2) " http://www.witheringtree.com/2011/09/custom-codeigniter-validation-methods/ ".

Is there any way to set the different custom messages for different fields? If so please give me the suggestion. There is already a file called MY_Form_validation in the application (which is mentioned in the second link) with some custom functions. Custom validations are triggering in that file except the custom function written by me. (I know you think may be I have written an faulty code! But there is only simple echo statement in that function. Just for testing only I have put an echo statement).

2
  • The first link you've provided is actually to a question that I provided the answer for!! Can you post some of the code you've already tried? That way it'll help to try and figure out why it's not working for you. Commented Jul 28, 2014 at 11:42
  • Hey Mark, The callback function is worked with some modifications to your answer. Those are removing _ from the beginning of the callback function and removed second _ between callback and < function_name >. So the code is changed as "function custom_required()" and the calling is "callback_custom_required()". Commented Jul 30, 2014 at 7:31

1 Answer 1

1

You wouldn't be able to create a generic function to do this as codeigniter has no way of knowing how the information was posted as it just receives it as an array.

What you could do is create MY_Form_validation.php in application/libraries

class MY_Form_validation extends CI_Form_validation
{

    public function required_select($val)
    {
        if ($this->required($val) === FALSE) {
            $this->set_message('required_select', 'Please select %s');
            return FALSE;
        }

        return TRUE;
    }

}

then when creating the rules for form validation

$this->form_validation->set_rules('dropdown', 'Dropdown', 'required_select');

Obviously, replace dropdown with the name of the element.

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer Ross. I tried the same in my code. But unfortunately. It didn't work out. I have placed a custom function in MY_Form_Validation.php page, where all other custom validation messages are placed. But the problem is control is not even going to that function. I have put an echo exit at the starting of the code. So I go with "Callback" functions. And it worked. Anyway again I will try to implement the custom function model like you said in the future, once I get enough time.
Hey man, I'm sorry to hear it didn't work, however, I'm not sure why as I use this all the time. This likes this could be down to case-sensitivity i.e. make sure that MY_F (and CI_F) are the only capital letters in both the file name and the class name, also I've just edited the set message line as it should be 'required_select'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.