Aftervalidate Function Not Working Correctly...

Something weird is going on in my set up. I have a "hasError" if block in my aftervalidate function and it returns true when there is no error and false when there is an error, which is the opposite of what I expect. Can anyone kindly have a look in case I am missing something here?

Here is my form:


<div class="form">

<?php $form = $this->beginWidget('CActiveForm', array(

      'id'=>'Search',

      'enableAjaxValidation'=>true,

      'htmlOptions' => array(

        'name' => 'Search',

        ),                

      'clientOptions' => array(

        'validateOnSubmit'=>true,   

        'validateOnChange'=>false,        

        'afterValidate'=>'js:function(form, data, hasError) {

          if(!hasError) {

          $("div.ajax-loader").dialog({ dialogClass: "noTitleStuff" });

            return true;

          }

        }'

        ),                                                                                                                                                  

      ));                                                                                                                                                       

?>  


<div style="text-align: center;">

<div style="padding: 2px 0px;"><?php echo $form->error($model, 'firstname'); ?></div>

<div style="padding: 2px 0px;"><?php echo $form->error($model, 'lastname'); ?></div>

</div>          


<div class="label" style="width: 95px;"> Search</div> 

<div class="row box">

<?php echo $form->textField($model,'firstname', array('id'=>'firstname')); ?>

<?php echo $form->textField($model,'lastname', array('id'=>'lastname')); ?>

<?php echo CHtml::submitButton('Search', array('class' => 'searchButton')); ?>

</div>                                        

<?php $this->endWidget(); ?>                   

</div> 



Here is my controller:


public function actionIndex()

{ 

  $model = new SearchForm;


  // collect user input data

  if(isset($_POST['SearchForm'])) {

    if(Yii::app()->getRequest()->getIsAjaxRequest()) {

      echo CActiveForm::validate(array($model));

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

    }                                                   


    $model->attributes = $_POST['SearchForm'];

    // validate user input                                          

    if ($model->validate()) {

        ...

    }                                                                                                                     

  }                                                                                                                           

  $this->render('index', array('model' => $model));

} 

And here is the model:


public function rules()

{                   

  return array(array('firstname, lastname', 'required', 'message'=>'Please enter {attribute}.'));

}                               


public function attributeLabels()   

{                                       

  return array(                                 

      'firstname' => 'First Name',                        

      'lastname' => 'Last Name'

      );                                                                                

}

I am running Yii on ubuntu and an apache webserver.

Hi obi1

Try


...

 if(hasError==false){

...



Hmmm

Thanks, but it didn’t work :(

check this wiki

Thanks for your reply Maggie. Our code was identical for the most part and mine still did not work.

I have however found something. It appears that because I am returning an array from my model by specifying that some variables are required in the rule, the ‘hasError’ variable always returns false. I have worked around this by checking if data is empty or not in my aftervalidate function. This now works for me:




'afterValidate'=>'js:function(form, data, hasError) {

     

                  if(jQuery.isEmptyObject(data)) { 

                    $("div.ajax-loader").dialog({ dialogClass: "noTitleStuff" });

                  }

                  return true;

                }'



the ‘hasError’ variable always returns false becuse in your code you can not add a clientOptions

like


 'enableClientValidation' => true,

                    'clientOptions' => array(

                            'validateOnSubmit' => true,

                    ),

if this include code ‘hasError’ return the true