'required' validation rule always fails

Hello,

I am trying to upload a file and it works fine when the input fields are populated properly by the user.

However, when the file field is left empty, an exception is thrown when I try to work with the file, because I have no validation rules for the field, other than a check to see whether it is a file, and everything fails if no file is specified.

When I include the file input field in the ‘required’ section of rules() in the model, the field is shown to be empty after submitting the form even when it is specified. The other fields of the form work fine, only the file input field fails.

Here are the rules:




public function rules()

    {

        return [

            [['name', 'keywords', 'gameSource', 'cover'], 'required'],

            [['keywords'], 'string'],

            [['name'], 'string', 'max' => 50],

            ['game_categories_id', 'safe'],


           [['gameSource', 'cover'], 'file'],

          //  [['gameSource', 'cover'], 'file', 'extensions' => 'jpg, png', 'mimeTypes' => 'image/jpeg, image/png',],

         ];

    }



This is the action:




 public function actionCreate()

    {

        $model = new Games();


        if ($model->load(Yii::$app->request->post()) && $model->save())

        {

            //TODO: handle empty field errors for gameSource and cover

           $game = UploadedFile::getInstance($model, 'gameSource');

           $gameFileName = time().'.'.$game->extension;

           $game->saveAs('games/'.$gameFileName);


            ...


            return $this->redirect(['view', 'id' => $model->id]);

        }

         else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }



The view:




 <?php $form = ActiveForm::begin([

        'options'=>[

            'class'=>'',

            'enctype'=>'multipart/form-data'

        ],

    ]); ?>


    <?= $form->field($model, 'name')->textInput(['maxlength' => 50]) ?>


    <?= $form->field($model, 'gameSource')->fileInput() ?>


    ...



Why is gameSource always empty when there is a ‘required’ rule for it, but works fine without it? The other fields, for which there is a ‘required’ rule, work fine.

Do you use a database to store the data?? If yes the field where you store the field-data uses the Not-Null attribute??

A MySQL database is used. The field does have a not-null attribute since it is a required field. I don’t think the database has anything to do with it, though. I think it has something to do with Yii’s validation rules.

Exception is thrown, because $game after this line


$game = UploadedFile::getInstance($model, 'gameSource');

equal NULL if file field is left empty. You should be check it:




$game = UploadedFile::getInstance($model, 'gameSource');

if (!is_null($game)) {

  $gameFileName = time().'.'.$game->extension;

  $game->saveAs('games/'.$gameFileName);

}



In regard to required:

What contains $_FILES after submitting form if file are selected?

I know how to handle the exception, but merely suppressing it is no good, because the rest of the process is meaningless without the file. That is why it has to be required. But making it a requirement breaks things.

A var_dump of $_FILES shows something like this:




...{ ["gameSource"]=> string(21) "blasbilliardsgold.swf"... 



after submitting the form. The user is returned to the form’s page afterwards and “empty field” is shown below the file input field. Nothing reaches the database.