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