php在验证表单时保留文件字段的值返回false [英] php keep the value of file fields when validating a form returns false

查看:21
本文介绍了php在验证表单时保留文件字段的值返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 yii 框架表单,我有一个这样的表单:

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

如果用户输入错误,文件字段变为空,然后用户必须重新选择他的图像..有没有办法将文件字段值保留在 yii 或 php 中?

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 ?

提前致谢

推荐答案

简短回答:否

文件输入依赖于用户的本地文件系统,服务器(因此 PHP)对此一无所知.因此,一旦表单已提交,用户将不得不重新选择文件.这更像是一个安全问题.

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:

  • 文件已成功上传,因此您可以将其保留在服务器上,并在重新提交表单时使用您的副本(难以实施,可能会使用户感到困惑,通常不推荐)
  • (更好)您可以在提交之前使用 Javascript 验证表单条目客户端 - 或通过 AJAX 服务器端验证.如果您这样做,您仍然应该在服务器端进行验证,但这将有助于为合法用户提供更好的用户体验,这似乎是您在这里所困扰的地方.

这篇关于php在验证表单时保留文件字段的值返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆