How to customize the error message of a validation rule

13 0
14
Viewed: 153 289 times
Version: Unknown (update)
Category: Tutorials
    Written by: qiang qiang
    Updated by: Yang He Yang He
    Created: Feb 4, 2009
    Updated: 14 years ago

    You are viewing revision #1 of this wiki article.
    This version may not be up to date with the latest version.
    You may want to view the differences to the latest version.

    next (#2) »

    Validators extending from [CValidator] all have a property named [message|CValidator::message]. You can set this property in the corresponding validation rule to customize the error message. For example, the following validation rule uses an error message that is different from the default one:

    class Post extends CActiveRecord
    {
    	public function rules()
    	{
    		return array(
    			array('title, content', 'required', 'message'=>'Please enter a value for {attribute}.'),
    			// ... other rules
    		);
    	}
    }
    

    In the above, the customized error message contains a predefined placeholder {attribute}. The [CRequiredValidator] (whose alias is required) will replace this placeholder with the actual attribute name that fails the validation.