In my Model (Images.php)
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('images_name', 'required'),
array('images_name', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>false, 'on'=>'update'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('images_id, fk_deal_id, images_priority, images_name, images_status', 'safe', 'on'=>'search'),
);
}
In my Controller
public function actionCreate()
{
$model=new Deals;
$model_details=new DealDetails;
$model_image = new Images;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation(array($model,$model_details,$model_image));
//$this->performAjaxValidation($model_details);
if(isset($_POST['Deals']))
{
$model->attributes=$_POST['Deals'];
$model_details->attributes=$_POST['DealDetails'];
$model_image->attributes = $_POST['Images'];
$rand_name = rand(100,9999);
$uploadedFile=CUploadedFile::getInstance($model_image,'images_name');
$fileName = "{$rand_name}-{$uploadedFile}";
$model_image->images_name = $fileName;
//$model_details->validate();
$validate = $model_image->validate();
$validate = $model_details->validate() && $validate;
$validate = $model->validate() && $validate;
if($validate){
$model_image->save(false);
$uploadedFile->saveAs(Yii::app()->basePath.'/../images/deal/'.$fileName);
$model->save(false);
$model_details::setDealId($model->deal_id);
$model_details->save(false);
$this->redirect(array('view','id'=>$model->deal_id));
}
}
$this->render('create',array(
'model'=>$model,
'model_details'=>$model_details,
'model_image'=>$model_image,
));
}
In My view
<div class="row">
<?php echo $form->labelEx($model_image,'images_name'); ?>
<?php echo $form->fileField($model_image, 'images_name'); ?> // by this we can upload image
<?php echo $form->error($model_image,'images_name'); ?>
</div>
Ajax Validation is working Fine, But when I submit the form without image the validation not working
Please Help me