Adding the "error" label to an element class after validation error

Hi to all!

I’m pretty new in Yii, and i can’t find the way to add the “error” label to an HTML element class after a validation error, to obtain the same behavior of the CRUD generated form when you don’t insert a required attribute in the form.

I made the form by myself for project reasons. Then, my situation is that i have a set of dropdownlist, each of them represents an object to be saved in the db.

When i validate them in the controller, i’m able to catch and display a possible error message by the addError() method on the instance of the current object, but i don’t know how set the class of the corresponding “dropdown” item, adding “error” to the class attribute of the dropdown.

Thank you!

Posting your code and pointing out what you are getting as well as what you are expecting will get you a long way.

Take a look at the documentation of CActiveForm::error.

In your view you can set the ‘errorCssClass’ in the htmlOptions:


echo $form->error($model,'dropdownName',array('errorCssClass'=>'error'));