Customised Error Messages

How can I display customised error messages in ajax enabled form. My form, model and Controller details are on following link.

http://www.yiiframework.com/forum/index.php?/topic/9218-form-fields-names-are-different-from-database-table-column-names/

I have enabled


$this->performAjaxValidation($model);

and using following code created function:


protected function performAjaxValidation($model)

    {

            if(1/*isset($_POST['ajax']) && $_POST['ajax']==='user-form'*/)

            {

                    echo CActiveForm::validate($model);

                    Yii::app()->end();

            }

    }



Thanks in advance

You can add a message property to your validation rules

http://www.yiiframework.com/doc/api/CValidator#message-detail

Example




array('order_date', 'required', 'message'=>'You have to add a date'),



/Tommy

Thanks for guidance. Iam getting following JSON response after changing “messsage” value of ‘wausername’ .


{"WaTabMod_wausername":["Username Required--"],"WaTabMod_wapassword":["Password cannot be blank."]}

My ‘rules’ function is as follows:




 public function rules(){

        return array(

                array('wausername', 'required', 'message'=> 'Username Required--'),

                array('wapassword', 'required'),

                array('wausername, wapassword', 'length', 'min' => 6),

            );

    }



Can some one guide me how to change ‘WaTabMod_wausername’ to ‘wausername’ in JSON response.

Thanks in advance

Why would you want to do that? ‘WaTabMod_wausername’ is the correct html field id for attribute ‘wausername’ of model ‘WaTabMod’.

/Tommy

@Tri: You can view from following link that I have changed ID and name of form fields:

http://www.yiiframework.com/forum/index.php?/topic/9218-form-fields-names-are-different-from-database-table-column-names/

Thanks

I don’t know the reason why you use custom html id/name - perhaps for access from js code - but a different approach might be to use CHtml::activeId($model, ‘someAttribute’) to generate the client-side id. Of course only an option if you publish your javascript from a Yii PHP-script.

HTH

/Tommy

@Blue Sapphire:

I guess you want to access your form elements from js and avoid those lengthy ids/names. Instead, you could add custom CSS class names to identify your input elements, and leave name+id untouched. Might feel odd at first, but it’s a perfectly legal thing to do and guaranteed to work in every browser. You could end up with selectors like:


$('.my-form .wausername');

$('.my-form .wapassword');