简体   繁体   English

php在验证表单返回false时保留文件字段的值

[英]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: 我正在使用yii框架表单,我有一个这样的表单:

<?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 ? 如果用户输入有错误,文件字段变空,然后用户必须重新选择他的图像..有没有办法将文件字段值保存在yii或php中?

Thanks in advance 提前致谢

Short answer: No 简答:不

A file input is reliant on the user's local file system, about which the server (and therefore PHP) knows nothing. 文件输入依赖于用户的本地文件系统,服务器(以及PHP)对此一无所知。 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: 以下是在这方面改进UX的两种可能方法:

  • 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. (更好)您可以在提交之前使用Javascript(或通过AJAX在服务器端)验证表单条目客户端。 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. 如果你这样做,你仍然应该验证服务器端,但它将有助于为合法用户提供更好的用户体验,这似乎是你在这里所困扰的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM