Php saves file field value when validating form returns false

I work with yii form forms and I have a form like this:

<?php echo CHtml::beginForm('', 'post', array('enctype' => 'multipart/form-data')); ?> Image:<?php echo CHtml::activeFileField($model, 'image');?> <?php echo CHtml::error($model, 'image');?> Full name: <?php echo CHtml::activeTextField($model, 'fullName');?> <?php echo CHtml::error($model,'fullName'); ?> <?php echo CHtml::submitButton('save);?> <?php echo CHtml::endForm();?> 

if when a user logs in, the error in the input field becomes empty, and then the user must re-select his image .. is there a way to save the value of the file field in yii or php?

Thank you in advance

+4
source share
1 answer

Short answer: No

The file entry depends on the user's local file system, which the server (and therefore PHP) knows nothing about. So, one form was submitted, the user will have to re-select the file. This is more of a security issue than anything else.

Here are two possible approaches to improving UX in this regard:

  • The file will be uploaded successfully, so you can save it on the server and use your copy when resubmitting the form (itโ€™s difficult to implement, potentially confusing the user, and is generally not recommended).
  • (it is better). You can test your client side using Javascript or server-side through AJAX - before sending it. If you do this, you should still check the back end, but this will help provide legitimate users with a better user interface that seems to bother you.
+10
source

Source: https://habr.com/ru/post/1390690/


All Articles