Hi all, would like to submit a form with ajaxsubmit button. I added ajax submit button in the form and added loading indicator. But when i try to submit form, its not showing the validation error in the form. How it can solve?
Controller
public function actionMyaction()
{
$model=new IllustrationMaster('test');
$this->performAjaxValidation($model);
if(isset($_POST['Master']))
{
$model->attributes=$_POST['Master'];
$valid=$model->validate();
if($valid){
//do anything here
echo CJSON::encode(array(
'status'=>'success'
));
Yii::app()->end();
}
else{
$error = CActiveForm::validate($model);
if($error!='[]')
echo $error;
Yii::app()->end();
}
}
$this->render('testcreate',array(
'model'=>$model,
));
}
testcreate.php
<?php echo $this->renderPartial('testform', array('model'=>$model)); ?>
testform
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
'action'=>$this->createUrl('master/Myaction'),
'enableClientValidation'=>true,
));
?>
<div class="errorMessage" id="formResult"></div>
<div id="AjaxLoader" style="display: none"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/loading.gif"></img></div>
<div class="row-user-single">
<?php echo $form->labelEx($model,'attribute1'); ?>
<?php echo $form->passwordField($model,'attribute1',array('size'=>60,'maxlength'=>500)); ?>
<?php echo $form->error($model,'attribute1'); ?>
</div>
<div class="row-user-single">
<?php echo $form->labelEx($model,'attribute2'); ?>
<?php echo $form->fileField($model,'attribute2',array('size'=>60,'maxlength'=>500)); ?>
<?php echo $form->error($model,'attribute2'); ?>
</div>
<div class="buttons">
<?php echo CHtml::ajaxSubmitButton('Save',CHtml::normalizeUrl(array('master/Myaction','render'=>true)),
array(
'dataType'=>'json',
'type'=>'post',
'success'=>'function(data) {
$("#AjaxLoader").hide();
if(data.status=="success"){
$("#formResult").html("form submitted successfully.");
$("#user-form")[0].reset();
}
else{
$.each(data, function(key, val) {
$("#user-form #"+key+"_em_").text(val);
$("#user-form #"+key+"_em_").show();
});
}
}',
'beforeSend'=>'function(){
$("#AjaxLoader").show();
}'
),array('id'=>'mybtn','class'=>'class1 class2')); ?>
<?php $this->endWidget();?>
Thanks