How to customize the error message of a validation rule

You are viewing revision #3 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 or see the changes made in this revision.

« previous (#2)next (#4) »

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 844 times
Version: Unknown (update)
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