Yii Framework Forum: Customised Error Messages - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Customised Error Messages Rate Topic: -----

#1 User is offline   Blue Sapphire 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 27-February 10

Posted 12 May 2010 - 09:31 AM

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

http://www.yiiframew...e-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
0

#2 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 12 May 2010 - 10:02 AM

View PostBlue Sapphire, on 12 May 2010 - 09:31 AM, said:

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

http://www.yiiframew...e-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.yiiframew...#message-detail

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


/Tommy

This post has been edited by tri: 12 May 2010 - 10:12 AM

Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   Blue Sapphire 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 27-February 10

Posted 12 May 2010 - 11:02 AM

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
0

#4 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 12 May 2010 - 11:22 AM

View PostBlue Sapphire, on 12 May 2010 - 11:02 AM, said:

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


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

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#5 User is offline   Blue Sapphire 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 27-February 10

Posted 12 May 2010 - 11:26 AM

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

http://www.yiiframew...e-column-names/

Thanks
0

#6 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 12 May 2010 - 12:34 PM

View PostBlue Sapphire, on 12 May 2010 - 11:26 AM, said:

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

http://www.yiiframew...e-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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 13 May 2010 - 03:41 AM

@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');

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users