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();
}
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.