File Field 'required' Validation

I am trying to ensure a file field is ‘required’ using ajax validation. I have the following:

View (simplified):


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'product-form',

	'enableAjaxValidation'=>true,

	'enableClientValidation'=>false,

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

		'validateOnChange'=>false,

	),

	'htmlOptions'=>array(

		'enctype'=>'multipart/form-data',

	),

)); ?>


<?php echo $form->errorSummary($model); ?>


<div class="row">

	<?php echo $form->labelEx($model, 'image_upload'); ?>

	<?php echo $form->fileField($model, 'image_upload'); ?>

	<?php echo $form->error($model, 'image_upload'); ?>

</div>


<div class="row buttons">

	<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

</div>


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


</div>

Model:




// public attribute

public $image_upload;


// validation rule

array('image_upload', 'required'),



The problem is, even when I have selected a file, it fails validation and the error message is displayed. It is not possible to proceed in the form.

I have also tried the ‘file’ validator using ‘allowEmpty’=>false but this does not work either.

I am using version 1.1.14

Anyone?

Hi

solution for your problem is here

http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/

http://www.yiiframework.com/doc/api/1.1/CFileValidator

you cannot use required rule for file field

Thanks. Is there any workaround?

I need the field to be marked as required (with asterisks) and also if the user does not select a file and just clicks on Submit it needs to show error.

Hi,

It can help you to solve ur problem

http://www.yiiframework.com/wiki/598/clientvalidation-for-files-modern-browsers/

To check for required condition

allowEmtpy set to false …then it is required field