Part of the EllisLab Network
   
 
Form Validation with Custom Message not displaying message.
Posted: 02 September 2010 09:35 PM   [ Ignore ]  
Grad Student
Rank
Total Posts:  36
Joined  10-15-2008

I have a simple form which I validate the input with:

$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’);) is the default CI error message

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.

Profile
 
 
Posted: 02 September 2010 11:10 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  361
Joined  02-02-2010

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:

<?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:

$this->form_validation->set_message('required''Your custom message here'); 
 Signature 

ViewsBoard - For Sharing information and making discussion

Twitter | Facebook | Linkedin

Profile
 
 
Posted: 03 September 2010 08:08 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  36
Joined  10-15-2008

Thank you.

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

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

Profile