Yii Framework Forum: validateOnSubmit not showing the error message - Yii Framework Forum

Jump to content

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

validateOnSubmit not showing the error message Rate Topic: -----

#1 User is offline   Renaud 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 04-October 11

Posted 01 August 2012 - 10:32 AM

Hi there,

I've got a form which is submitted with an ajax button. It works well when there is no error, the data is saved and the content is updated asynchronously.

My problem is with the validation, for example if a field is blank. I can see that the error is triggered but the error message is not displayed on the form. Here is my code:

View
<div id="service_user">

    <?php
    $form = $this->beginWidget('CActiveForm', array(
                'id' => 'serviceUser-form',
                'enableAjaxValidation' => true,
                'clientOptions' => array('validateOnSubmit' => true),
            ));
    ?>

    echo CHtml::ajaxSubmitButton('Save', CHtml::normalizeUrl(array('maintainClient/saveUser)), array(
        'beforeSend' => 'function() { }',
        'success' => 'function(data) {

                    if (data.indexOf("{")==0) {
                          //data contains  {"ServiceUser_first_name":["First name cannot be blank."]}
                          alert("validation error");
                          
                          $("#service_user").append(data);
                    }
                    else {
                        
                          //alert("validation successful");
                    
                          $("#service_user").replaceWith(data);
                          displayMode();
                          enableAllButtons();
                    }
                    
                    }',
        'error' => 'function(data) {
                   
    }',
            ), array('class' => 'rc-button', 'id' => 'save')
    );


Controller
 public function actionSaveUser() {

        if (!empty($_POST['ServiceUser'])) {

            $serviceUser = new ServiceUser();

            $serviceUser->attributes = $_POST['ServiceUser'];

            if (!$serviceUser->save()) {

                $errorMessage = CActiveForm::validate($serviceUser);
                echo $errorMessage;
            } else {

                $output = $this->renderPartial('_service_user', array(
                            'serviceUser' => $serviceUser,
                                ), true);

                echo $output;
            }
        }


My problem is that the returned error message (e.g {"ServiceUser_first_name":["First name cannot be blank."]} is not plugged to the error field.

Any idea ?

Cheers
Renaud
0

#2 User is online   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,110
  • Joined: 05-January 12
  • Location:Morocco

Posted 01 August 2012 - 11:06 AM

I believe when there's an error, the data returned by CActiveForm::validate() is in JSON format.

So, so you should deal with it in your Ajax request:
'success' => 'function(data) {
    if (data.indexOf("{")==0) {
        var json = $.parseJSON(data);
        $.each(json, function(key, value) {
            $("#" + key).addClass("clsError");
            $("#" + key + "_em_").show().html(value.toString());
            $("label[for=" + key + "]").addClass("clsError");
        });
    }
    …

0

#3 User is offline   Renaud 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 04-October 11

Posted 01 August 2012 - 11:10 AM

Thank you so much Bennouna, it did the trick !

Renaud
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