Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php keep the value of file fields when validating a form returns false

I'm working with yii framework 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 there's an error in user input the file field becomes empty and then the user has to re-select his image.. is there a way to keep the file field value in yii or php ?

Thanks in advance

like image 571
NadaNK Avatar asked Dec 28 '22 08:12

NadaNK


1 Answers

Short answer: No

A file input is reliant on the user's local file system, about which the server (and therefore PHP) knows nothing. So one the form has been submitted, the user will have to reselect the file. This is more of a security issue than anything else.

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

  • The file will have been successfully uploaded, so you could keep it on the server and use your copy when the form is resubmitted (hard to implement, potentially confusing to the user and generally not recommended)
  • (better) You could validate the form entry client side with Javascript - or server side via AJAX - before it is submitted. If you do this, you should still validate server-side, but it will help give legitimate users a better user experience, which seems to be what you are bothered about here.
like image 53
DaveRandom Avatar answered Jan 31 '23 07:01

DaveRandom