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

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

问题描述

 <?php echo CHtml:

我正在使用yii框架表单, :beginForm('','post',array('enctype'=>'multipart / form-data')); ?>
Image:<?php echo CHtml :: activeFileField($ model,'image');?>
<?php echo CHtml :: error($ model,'image');?>

全名:<?php echo CHtml :: activeTextField($ model,'fullName');?>
<?php echo CHtml :: error($ model,'fullName'); ?>
<?php echo CHtml :: submitButton('save);?>
<?php echo CHtml :: endForm();?>

如果用户输入中有错误,则文件字段变空,然后用户必须重新选择他的形象..有没有办法保持文件字段的值在YUI或PHP?

提前致谢



在这方面,有两种可能的改进UX的方法:


  • 文件将被成功上传,因此您可以将其保留在服务器上,并在表单重新提交时使用您的副本(难以实现,可能会让用户感到困惑,一般不推荐)

  • (更好)您可以在提交之前使用Javascript验证表单条目客户端,或者通过AJAX验证服务器端。如果你这样做,你仍然应该验证服务器端,但它会帮助给合法的用户一个更好的用户体验,这似乎是你在这里打扰。

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

解决方案

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.

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

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