How to customize the error message of a validation rule

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.

13 0
14 followers
Viewed: 145 803 times
Version: 1.1
Category: Tutorials
Tags:
Written by: qiang
Last updated by: Yang He
Created on: Feb 4, 2009
Last updated: 12 years ago
Update Article

Revisions

View all history