Ajax Validation and fileField error

Hi,

I have a form that has a file field, the file is required so I’m setting ‘allowEmpty’ => ‘false’. The problem is that the Ajax Validation keep telling that field is no valid even after selecting a file.

How to solve this?

View




<?php

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

        'id' => 'file-form',

        'enableAjaxValidation'=>true,

        'clientOptions'=>array('validateOnSubmit'=>true, 'hideErrorMessage'=>true),

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

            ));

    ?>

.....

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



Controller




public function rules() {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array(

            array('name', 'required'),

            array('name', 'length', 'max' => 250),

            array('file_path', 'file', 

                'types'=>'pdf,txt,doc,docx', 

                'maxSize'=>1024 * 1024 * 50, 

                'allowEmpty' => false),

            array('id, name, file_type, file_path', 'safe', 'on' => 'search'),            

        );

    }



Thank you

for uploading the file validate can’t use the ajax feature . because you can’t use ajax to upload a file

(although there is a ajaxUpload(ajaxupload))but the internal is use some trick ways .

so in client the only thing for uploading you can do is the size( unless you use a flash then you can validate the type and the size) you can look at the source code of the CFileValidator or the CUploadedFile

for details :lol:

Hello, I am having the exact same problem :slight_smile:

But my problem is that I still want to have an ajax Validation for my form (even if it doesn’t validate the file, this will be done server-side)

Is it possible to ignore the file validation when it is an ajax validation (So there won’t be the error) ?

Thanks :)

You could change the scenario of the model when you do Ajax validation, and define your required rules so that they don’t apply to this scenario.

Well, this is working great !

Thank you :)

Had same problem - couldn’t set up different scenario for ajax-only validation, but this nice approach helped me