Display attribute label in validator error message

hey, guys!

i need some input values not to be negative, like prices and discount values, so i created a custom validation function to check the rule, as following:




	public function notNegative($attribute){

		if (0 > $this->$attribute):

			$message = Yii::t('yii', '{attribute} cannot be negative!', array('attribute'=>$attribute));

			$this->addError($attribute,$message);

		endif;

	}



in rules, i have so:




...

array('offerPriceInCash','notNegative'),

...



ok, valitation till now works, but i can’t figure out how to get the attribute label instead of the attribute name in the error message…i’ve created the translation in my yii.php message file and i’m getting something similar to

{offerPriceInCash} não pode ser negativo! --> note that Yii::t() translates the message correctly, but what about attribute label?

could somebody please give me a hand on thhis? am i missing something?

thanks in advance!

regards!

:)

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#getAttributeLabel-detail

Thanks, man!

final code:




	public function notNegative($attribute){

		if (0 > $this->$attribute):

			$message = Yii::t('yii', '{attribute} cannot be negative!', array('{attribute}'=>$this->getAttributeLabel($attribute)));

			$this->addError($attribute,$message);

		endif;

	}




in rules array:




...

array('offerPriceInCash, offerPriceBefore, offerDiscountPercent','notNegative'),

...



:)

regards!

Note that this you can achieve with the CNumberValidator like




array('offerPriceInCash, offerPriceBefore, offerDiscountPercent','numerical','min'=>0,'tooSmall'=>'{attribute} cannot be negative!'),



yeah…you’re right! it’s quite simple! i’ll certainly use that

:)

thank you very much

regards!