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.