'allowempty' => False Not Working

Hi

I have a file upload that is required as part of a form. I have the following rules




array('file', 'file', 'on' => 'insert', 

            'allowEmpty' => false, 

            'safe'=>true,

            'maxSize'=> 512000, 

            'maxFiles'=> 1, 

            'mimeTypes' => 'application/msword, text/plain',

            'tooLarge'=> 'file cannot be larger than 500KB.',

            'wrongMimeType'=> 'Format must be: .doc .txt'

        ),



However when I leave the file upload blank it still tries to process the form.




<?php $form = $this->beginWidget('GxActiveForm', array( // Giix extension

	'id' => 'file-form',

	'enableAjaxValidation' => false,

	'htmlOptions'=>

		array('enctype'=> 'multipart/form-data'),


));

?>

		<div class="row">

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

		<?php echo $form->textField($model, 'name', array('maxlength' => 80)); ?>

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

		</div><!-- row -->

		<div class="row">

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

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

		</div><!-- row -->




What am I doing wrong?

Thanks for any help

[size=2]Could you please try with this code.[/size]




array('file', 'file', 'on' => 'insert', 

            'allowEmpty' => false, 

            //'safe'=>true,

            'maxSize'=> 512000, 

            'maxFiles'=> 1, 

            'mimeTypes' => 'application/msword, text/plain',

            'tooLarge'=> 'file cannot be larger than 500KB.',

            'wrongMimeType'=> 'Format must be: .doc .txt'

        ),



Hi Codesutra,

Tried your code, same result :huh:

Its strange. :unsure:

Can you please show your complete code for model rules?

What scenario have you given in the controller for the model.

Just add after $model declaration in the controller action as you have defined the validation rule for the scenario insert.

$model->scenario=‘insert’;

Let me know if it has solved your problem

I have a similar problem.

Anyone knows what’s wrong?

ok make sure that yo do the following

1- define the scenario eg:


$model->scenario = 'insert';

2- validate the form eg:




if(isset($_POST['form']){

if($model->Validate()){

//complete process

}

}

Adding a validate() call after the attributes have assigned seemed to clear up my problem. Although I did think the save() method automatically called the validate() method.