File validation


(Vytautas Bartkevicius) #1

Hello, I have problem with file validation

View:

<?php $form = $this->beginWidget(‘CActiveForm’, [

    'id' =&gt; 'help-form',


    'enableAjaxValidation' =&gt; false,


    'htmlOptions' =&gt; array('enctype' =&gt; 'multipart/form-data'),

]); ?>

<?= $form->fileField($model, ‘receipt_copy’); ?>

Model rules:

[‘receipt_copy’, ‘file’, ‘allowEmpty’=>true, ‘types’=>‘jpg, gif, png’, ‘message’ => ‘wrong format’],

Other fields work normal by specified rules, but receipt_copy file field in not working. Why? When I add txt file and submit no errors is showing and form passes.

When i delete ‘allowEmpty’=>true form never passes and all the time the error is showing, so maybe someone can explain why the file is not recognized?

Edit:

Controller:

if (isset($_POST[‘TestForm’]))

{

&#036;model-&gt;attributes = &#036;_POST['TestForm'];


&#036;model-&gt;receipt_copy= CUploadedFile::getInstance(&#036;model, 'receipt_copy');


if (&#036;model-&gt;validate()) 


{


  &#036;model-&gt;save();


}

}

model is saved, receipt copy = null.


(swapn) #2
  • enable ajax validation and use multipart/form-data
  • get file instance and validate model
  • $_FILES used on post file that time your trace it file was selected or not
  • if yes then validate also and save also