File validation

Hello, I have problem with file validation


<?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?



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()) 





model is saved, receipt copy = null.

  • 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