Hi,
I am submitting a CActiveForm with an ajaxSubmitButton.I have set enableClientValidation to true-(doesnt matter ,the problem is the same with both enableAjaxValidation and enableClientValidation set to true).So,clientvalidation works as expected.For example leaving a required field empty,validation turns the input into red,and the error message appears.Now when I click the submit button,the ajax POST submission is made,even if the form did not validate.Why?Here is my form:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'comment-form',
// 'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($model,'author'),
'clientOptions'=>array('validateOnSubmit'=>true,
'validateOnType'=>false,
),
)); ?>
<?php echo $form->errorSummary($model); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'author'); ?>
<?php echo $form->textField($model,'author',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'author'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'url'); ?>
<?php echo $form->textField($model,'url',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'url'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'content'); ?>
<?php echo $form->textArea($model,'content',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'content'); ?>
</div>
<div class="row">
<?php echo $form->hiddenField($model,'article_id',array('value'=>$article_id)); ?>
</div>
<div class="row buttons">
<?php
// FB::setEnabled('false');
// echo CHtml::submitButton($model->isNewRecord ? 'Submit' : 'Save');
echo CHtml::ajaxSubmitButton( $model->isNewRecord ? 'Submit' : 'Save',
$this->createUrl('article/saveComment'),
array('type'=>'POST',
'success'=>'function(data) {
var response= jQuery.parseJSON (data);
if (response.success ==true){
$("#ajaxstatus").append(response.message)
.addClass("flash-success")
.fadeToggle(2000, "linear");
$("#comment-form").slideToggle(1500);
}
else{
$("#ajaxstatus").hide().
addClass("flash-error")
.append(response.message).fadeIn(2000);
$("#comment-form").each (function(){
this.reset();
});
}
}' //success
),
array('type'=>'submit')
);
?>
</div>
<?php $this->endWidget(); ?>
I can see in my firebug console that the ajax POST call to action article/saveComment is made,even if validation fails(required fields are empty).Why?
Thanks in advance,I will appreciate your help.