Yii Framework Forum: ajax validation in yii - Yii Framework Forum

Jump to content

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

ajax validation in yii Rate Topic: -----

#1 User is offline   Mohammed Iqbal Khan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 14-April 15
  • Location:India

Posted 01 September 2015 - 03:57 AM

I created a ajax submit button to submit a form.


The form is working Good and all the functionalty which I need working perfectly.


I enabled Ajax validation and a success message also. The problem occurs when I partial redirect the form.


The ajax function and validation is not working.

Controller

public function actionCreate()
{
    $model=new Comments;

    // Uncomment the following line if AJAX validation is needed
    $this->performAjaxValidation($model);

    if(isset($_POST['Comments']))
    {
        $model->attributes=$_POST['Comments'];
        // echo '<pre>';print_r($model->attributes);die();
        $valid = $model->validate();

        if($valid){
            if($model->save()){
            echo CJSON::encode(array('status'=>'success'
                ));
        }
        }else{
            $error =CActiveForm::validate($model);
            if($error != '[]')
                echo $error;
            Yii::app()->end();
        }
    }
}


View/_form

<?php 
$form = $this->beginWidget('CActiveForm',array('id'=>'comments-form',
    'enableAjaxValidation'=>true,
    'action'=>$this->createUrl('Comments/create'),
    'enableClientValidation'=>true
    ));
?>
<div class="errorMessage" id="formResult"></div>
<div id="AjaxLoader" style="display:none" >
<img src="<?php echo Yii::app()->request->baseUrl;?>/theme/images/spinner.gif">
</div>
<div class="row-user-single-user-single" >
        <?php echo $form->labelEx($model,'type'); ?>
        <?php  
         echo $form->dropDownList($model,'type',
         array(""=>"Select Type","0"=>"Offer","1"=>"Events"),
         array('style' => 'width:220px;','class'=>'form-control','disabled'=>false,)); ?>
        <?php echo $form->error($model,'type'); ?>

</div>
<div class="row-user-single-user-single" >
        <?php echo $form->labelEx($model,'offereventid'); ?>
        <?php echo $form->textField($model,'offereventid',array('style' => 'width:500px;','class'=>'form-control','disabled'=>false,)); ?>
        <?php echo $form->error($model,'offereventid'); ?>
</div>
<div class="row-user-single-user-single">
        <?php echo $form->labelEx($model,'name'); ?>
        <?php echo $form->textField($model,'name',array('style' => 'width:500px;','class'=>'form-control','disabled'=>false,)); ?>
        <?php echo $form->error($model,'name'); ?>
</div>
<div class="row-user-single">
        <?php echo $form->labelEx($model,'email'); ?>
        <?php echo $form->textField($model,'email',array('style' => 'width:500px;','class'=>'form-control','disabled'=>false,)); ?>
        <?php echo $form->error($model,'email'); ?>
    </div>

    <div class="row-user-single">
        <?php echo $form->labelEx($model,'comment'); ?>
        <?php echo $form->textArea($model,'comment',array('style' => 'width:500px;','class'=>'form-control','disabled'=>false,)); ?>
        <?php echo $form->error($model,'comment'); ?>
    </div>

    
    <div class="buttons">
    <?php
        echo CHtml::ajaxSubmitButton('Submit',CHtml::normalizeUrl(array('Comments/create','render'=>true)),
            array(
                'dataType'=>'json',
                'type'=>'post',
                'success'=>'function(data){
                    $("#AjaxLoader").hide();
                    if(data.status == "success"){
                        $("#formResult").html("Comment Submitted");
                        $("#comments-form")[0].reset();
                    }else{
                        $each(data,function(key.val){
                            $("#comments-form #"+key+"_em_").text(val);
                            $("#comments-form #"+key+"_em_").show();
                        });
                    }
                }',
                'beforeSend'=>'function(){
                    $("#AjaxLoader").show();
                }'
                ),array('id'=>'submit','class'=>'btn btn-success'));

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

    </div>

Can any one help me with this....
MIK
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