Uploaded file allowEmpty rule only in a certain scenario

Hey, guys, I’m trying to do this:

I have a form in which users can create or update data. In this form I have a file which should be required only in creation time, not in update scenario.

I’ve tried this:

array(‘upFile’, ‘file’, ‘types’=>‘jpg,JPG’,‘allowEmpty’=>true ,‘wrongType’=>‘Only jpg allowed’,‘on’=>‘update’),

array(‘upFile’, ‘file’, ‘types’=>‘jpg,JPG’,‘allowEmpty’=>false ,‘wrongType’=>‘Only jpg allowed’,‘on’=>‘create’),

And in my controller/action update:

$model->scenario = ‘update’;

controller/action create:

$model->scenario = ‘create’;

I also tried this:

array(‘upFile’, 'required, ‘on’=>‘create’);

array(‘upFile’, ‘file’, ‘types’=>‘jpg,JPG’,‘allowEmpty’=>true ,‘wrongType’=>‘Only jpg allowed’,‘on’=>‘update’),

But for both update and create actions I got the message like “file can’t be blank…”

Could you please tell me what I’m doing wrong?

Thank you very much

Nobody?

Hi, Sidebar!

Got no answers, I ended up removing the update action and using the following code in my model rules:




array('upFile', 'file', 'types'=>'jpg,JPG','allowEmpty'=>false,'maxSize'=>5242880,'wrongType'=>'Only jpg allowed.','tooLarge'=>'File too large! 5MB is the limit'),



Fortunatelly, in this case it was possible not to have the update action and I discovered it after revising the proccess.

After that, I start using flash uploaders as uploadify and fancyupload (both in extensions page). I’ve not been using the file validation rule since that tragic episode…heheh

:)

Best regards!

I had the same problem…

These are my rules:




array('link', 'file', 'types'=>'pdf,doc,xls,txt','maxSize'=>1024*1024*5,'on'=>'insert'),

array('link', 'file', 'allowEmpty'=>true,'types'=>'pdf,doc,xls,txt','maxSize'=>1024*1024*5,'on'=>'update'),



In the actionUpdate() before calling save I just test if a new file has been uploaded and if it’s not I save only other attributes:




$model->save(true,array('name','email'); // all other attributes without the link



I also facing same problem, 1.1.7 version already solve this problem?

I solved the problem just now. If you declare the rules for file few times, the upper rule will override below rule. So, you should declare like


public function rules() {

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

        // will receive user inputs.

        return array(

            array('title', 'required'),

            array('title', 'length', 'max'=>125),

            array('description', 'safe'),

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

                'types'=> 'pdf',

                'maxSize' => 1024 * 1024 * 10, // 10MB                

                'tooLarge' => 'The file was larger than 10MB. Please upload a smaller file.',                

            ),

            array('pdf', 'file','on'=>'update',

                'allowEmpty' => true,

                'types'=> 'pdf',

                'maxSize' => 1024 * 1024 * 10, // 10MB                

                'tooLarge' => 'The file was larger than 10MB. Please upload a smaller file.',                

            ),

        );

    }

rather than


public function rules() {

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

        // will receive user inputs.

        return array(

            array('title', 'required'),

            array('title', 'length', 'max'=>125),

            array('description', 'safe'),

            array('pdf', 'file','maxSize' => 1024 * 1024 * 10, // 10MB                

                'tooLarge' => 'The file was larger than 10MB. Please upload a smaller file.',                

            ),

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

                'allowEmpty' => false,                             

            ),

            array('pdf', 'file','on'=>'update',

                'allowEmpty' => true,                             

            ),

        );

    }