0

I have created a form using CActiveForm in yii. I am using ajax to submit the form using CHtml::ajaxSubmitButton. Form submission using ajax is working correctly and I am able to get the values in the control using $_POST['modelname']['fieldname']. But the ajax form validation is not working correctly. Below is the code

<?php 
    $form=$this->beginWidget('CActiveForm', array(
    'id'=>'item-brand-form',
    'enableAjaxValidation' => true,
    'clientOptions'=>array(
        'validateOnSubmit'=>true,
     ),
    'htmlOptions'=>array(
        'class'=>'form-horizontal',
    ),
)); ?>

<div class="form-group">
<?php echo $form->labelEx($model,'Brand Name',array('class'=>'control-label col-lg-4','for'=>'ItemBrand_brand_name')); ?>
<div class="col-lg-6">  
<?php echo $form->textField($model,'brand_name',array('class'=>'form-control')); ?>  
<?php echo $form->error($model,'brand_name'); ?>            
</div>
<!-- End col-lg-6 -->
</div><!-- End Form Group -->


<div class="form-group">
<?php echo $form->labelEx($model,'Brand Code',array('class'=>'control-label col-lg-4','for'=>'ItemBrand_brand_code')); ?>
<div class="col-lg-6">                  
<?php echo $form->textField($model,'brand_code',array('class'=>'form-control')); ?> 
</div>
<!-- End col-lg-6 -->
</div><!-- End Form Group -->


<div class="form-group">
<div class="col-lg-4"></div>
<div class="col-lg-2">
<?php echo CHtml::ajaxSubmitButton('Apply',array('class'=>'btn btn-lg btn-primary')); ?>
</div>
</div>
<?php $this->endWidget(); ?>

Also in the controller action following code is not working.

if(isset($_POST['ajax']) && $_POST['ajax']==='item-brand-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

so instead I am using

if(Yii::app()->getRequest()->getIsAjaxRequest()) 
        {
        echo CActiveForm::validate( array( $model)); 
        Yii::app()->end(); 
        }

While checking in the browser I am getting the response

{"ItemBrand_brand_name":["Brand Name cannot be blank."]}

But the error message is not displaying in the form. Also why the $_POST['ajax'] is not working ? Please help. Thanks in advance.

5
  • 1
    add <?php echo $form->errorSummary($models); ?> in your form. Commented Jan 24, 2014 at 6:13
  • @kumar_v I tried with that,but it is not working. Commented Jan 24, 2014 at 6:27
  • try with client validation true Commented Jan 24, 2014 at 6:37
  • with 'enableClientValidation'=>true, it is showing error when focus goes out of 'brand_name' field. It is not showing error on form submission. Commented Jan 24, 2014 at 6:46
  • @BabakBandpay Yes. Please see the answer below. Commented Aug 30, 2014 at 1:51

1 Answer 1

1

I have solved this by using CHtml::submitButton instead of CHtml::ajaxSubmitButton and then submitting the form using jQuery $.ajax function in the form 'afterValidate' option. Please see the details here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.