CodeIgniter Forums

Full Version: Issue - Form_Validation > Setting Error Messages > set_rule()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Guys,
Could someone please give a Light what's going wrong here please?

I try to set a custom error message for a particular field on some particular rule, but when i call the rule, i still see the native error messages from the file system/language/english/form_validation_lang.php.

I've checked other Posts from another users, but i didn't find the correct answer for that. I try to do exactly what the documentation says, but till now it doesn't work.

Documentation:
https://www.codeigniter.com/userguide3/l...r-messages

"If you need to set a custom error message for a particular field on some particular rule, use the set_rules() method:"


Code:
$this->form_validation->set_rules('field_name', 'Field Label', 'rule1|rule2|rule3', array('rule2' => 'Error Message on rule2 for this field_name'));

This is my Post Method.


Code:
    // CREATE NEW POST FORM
    public function create()
    {
        //******* THE FUNCTIONS MUST BE LOADED IN EVERY METHOD *******//
        // Load the Function php echo base_url();
        $this->load->helper('url');
        // Loading a Model Locally
        $this->load->model('Post_model');
        // Loading a Form Helper Locally
        // Load the Function php echo base_url();
        $this->load->helper(array('form', 'url'));
        // Loading Form Validation Library Locally
        $this->load->library('form_validation');
        //******* THE FUNCTIONS MUST BE LOADED IN EVERY METHOD *******//
        
        $data['title'] = 'Create Post';

        $this->form_validation->set_rules('title', 'Post Title', 'trim|min_length[5]|max_length[85]|required', array('required' => 'O campo Título deve ter minimo 5 caracteres.'));

        $this->form_validation->set_rules('body', 'Body', 'trim|required|min_length[5]|max_length[1000]');


        // IF THE LIBRARY "FORM VALIDATION" RECOGNIZE THAT ONE
        // OF THE RULES IS NOT RESPECTED THEN STAY ON http://localhost/dogs/posts/create
        if ($this->form_validation->run() === FALSE)
        {
                $this->load->view('templates/header');
                $this->load->view('posts/create', $data);
                $this->load->view('templates/footer');        

        } else {
        // ELSE...EXECUTE THE POST_MODEL METHOD create_post & REDIRECT TO http://localhost/dogs/posts
                $this->Post_model->create_post();
        // REDIRECT TO http://localhost/dogs/posts
                redirect('posts');
        }                
    }

And this is my view Form file


Code:
<!-- FORMNAME -->
<h2><?= $title; ?></h2>

<!-- GENERATE FORM VALIDATION ERROR MESSAGES -->
<?php echo validation_errors(); ?>

<!-- FORM CREATEION -->
<?php echo form_open('posts/create'); ?>

  <div class="form-group">

    <label>Post Title</label>
    <input type="text" class="form-control" id="#" aria-describedby="#" name="title" placeholder="Post Title">

    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>

  </div>

  <div class="form-group">

    <label>Post Content</label>
    <textarea class="form-control" id="#" name="body" placeholder="Write Post Content"></textarea>

  </div>
   <button type="submit" class="btn btn-primary">Submit</button>

</form>

I thank you in Advance for any Help or Light.
What version of CodeIgniter are you running? I tried out your rule and I see the custom message in 3.1.10.

For the heck of it, have you tried overriding the whole form_validation with
PHP Code:
$this->form_validation->set_message('required''Test message.'); 
or something similar? Just for testing purposes of course. Dependent on that result, it helps steer you in the right direction with what might be the issue.
I agree with xenomorph1030 in the previous post. I too added your exact error message override to one of my form validation rules for a field and it gave your error message as expected.

PHP Code:
$this->form_validation->set_rules('name''Name''trim|required|min_length[3]|max_length[100]', array('required' => 'O campo Título deve ter minimo 5 caracteres.')); 

Not much help sorry.

Paul
(02-20-2019, 10:39 AM)xenomorph1030 Wrote: [ -> ]What version of CodeIgniter are you running? I tried out your rule and I see the custom message in 3.1.10.

For the heck of it, have you tried overriding the whole form_validation with
PHP Code:
$this->form_validation->set_message('required''Test message.'); 
or something similar? Just for testing purposes of course. Dependent on that result, it helps steer you in the right direction with what might be the issue.

I'm sorry, i'm trying again, and again and again and i don't get the message running!

Another way to do the message to run is like that:


PHP Code:
$this->form_validation->set_rules('name''Name''trim|required|min_length[3]|max_length[40]', ['min_length' => 'O campo Nome deve ter pelo menos 3 caracteres de comprimento.']); 

But really, i still have difficulties to see what's going wrong with this message or what is missing in my configuration.



PHP Code:
$this->form_validation->set_rules('name''Name''trim|required|min_length[3]|max_length[100]', array('required' => 'O campo Título deve ter minimo 5 caracteres.')); 

And yes, i use as the documentation says the set_message() in a condition IF


PHP Code:
$this->form_validation->set_message('rule''Error Message'); 


What should i try again please? Thank you!
Dear  xenomorph1030 & PaulD

I got it running! But i did a small change in the code.


PHP Code:
$this->form_validation->set_rules('name''Name''trim|required|min_length[5]|max_length[40]', array('min_length' => 'O campo Nome deve ter pelo menos 5 caracteres de comprimento.')); 
I use as Trigger the rule 'min_length'! If i use the rule 'required' i will get the standard message from the file english/form_validation_lang.php in english.

Thank you so much Guys! I appreciate that!