Welcome Guest, Not a member yet? Register   Sign In
Form Validation with Custom Message not displaying message.
#1

[eluser]escape[/eluser]
I have a simple form which I validate the input with:

Code:
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name','FIRST NAME','required');

$this->form_validation->set_message('first_name', 'My Error Message');  
    
if ($this->form_validation->run() == FALSE)
{    
$err['error_first_name'] = form_error('first_name');
}

The form catches the validation rule, however the message displayed (via $err['error_seller_first_name'] = form_error('first_name')Wink is the default CI error message
Quote:The FIRST NAME field is required.

I was expecting the custom error 'My Error Message' to be displayed, but that doesn't seem to be the case.

What am I missing here? :question:

Thanks

BTW: $err['error_first_name'] is being parsed in the posting form.
#2

[eluser]mi6crazyheart[/eluser]
If u want change any error message for any particular input element of form, then i think u need to use a callback function & define u'r error message at there.

Ex:
Code:
<?php

class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('form_validation');
            
        $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');
                    
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }
    
    function username_check($str)
    {
        if ($str == 'test')
        {
            $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
    
}
?>

If u want to override any error message found in the language file. For example, to change the message for the "required" rule you will do this:
Code:
$this->form_validation->set_message('required', 'Your custom message here');
#3

[eluser]escape[/eluser]
Thank you.

I did not realize the first argument in the set_message function referenced the specific rule as in 'required'.

Code:
this->form_validation->set_message('required', 'Your custom message here');

Now I know why many of the discussions in this forum were trying to create error messages for multiple rules.

Much clearer now.

Best




Theme © iAndrew 2016 - Forum software by © MyBB