Dynamic Error Messages In Validators

Does someone know an example of adjusting validators’ error messages dynamically? Something like via a callback.

For example, I have a validator for uniqueness of certain fields in DB, and error message is currently defined something like this:

'message' => 'Such a record does exist already'

But I’d like the message to contain a link to the existing record. Of course, validator “knows” invalid $object and all its fields values. The problem is that this info is available in the validator’s validateAttribute method only, not outside. If it could be passed via callback to validator’s host, then it can be used to call findByAttributes and get model ‘id’, then to construct url for this existing model.

Thanks in advance.

check this


i hope it’s some help.

Looks great. I’ll give it a try. Thank you.


 public function rules() {

        $message ="". Yii::t('app','{attribute} cannot be blank.')."";

        return array(

           array('firstname, lastname, email, username','required','message'=>$message),



The first your suggestion (posted earlier) - works. It’s universal.

As for this second one, I’m not sure, how can one calculate {attribute} value dynamically in reponce to validation process.