Welcome Guest, Not a member yet? Register   Sign In
Issue - Form_Validation > Setting Error Messages > set_rule()
#1

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.
Reply


Messages In This Thread
Issue - Form_Validation > Setting Error Messages > set_rule() - by Porto - 02-19-2019, 09:14 AM



Theme © iAndrew 2016 - Forum software by © MyBB