【问题标题】:FileUpload required="true" doesn't work in PrimeFacesFileUpload required="true" 在 PrimeFaces 中不起作用
【发布时间】:2014-12-23 21:33:35
【问题描述】:

我正在尝试将验证放在我的 <p:fileUpload> 中。当用户上传而不放置任何文件时,他会收到一条错误消息。我正在使用mode="simple"required="true"required="true" 不起作用。

P.S:我需要使用mode="simple",因为我需要<p:commandButton>来提交其他数据。

<p:panelGrid columns="2">
    <h:outputLabel id="image" value="Select Image: *" />

    <p:fileUpload value="#{Jcalendar.file}" mode="simple"
                  allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                  required="true"
                  requiredMessage="File not selected !!"/>

    <f:facet name="footer">
        <p:commandButton value="Submit"
                         ajax="false"
                         action="#{Jcalendar.Upload}"
                         update=":form:msgs" />
    </f:facet>
</p:panelGrid>

【问题讨论】:

  • 包括您自己的自定义验证器在内的任何验证器都不能使用&lt;p:fileUpload&gt;
  • @Tiny 你能告诉我怎么做吗?我是primefaces的新手
  • 除了将验证逻辑放入&lt;f:fileUpload&gt; 的侦听器(或可能某处)之外,别无他法(除非制作了一些乏味的黑客/本土开发者)。 See for example(和maybe)。
  • 忘记考虑您使用的是mode="simple"。您可能有机会实现自定义验证器(我从未使用过mode="simple")。
  • @Tiny 他们都使用 mode="advanced" 但我不能使用它,因为我不想使用(FileUploadEvent 事件)

标签: jsf file-upload jsf-2 primefaces


【解决方案1】:

根据 5.2 源代码,当使用 Servlet 3.0 本机上传而不是 Apache Commons FileUpload 时,这确实会失败。

NativeFileUploadDecoder#decodeSimple() 只会在输入根本没有提交时将提交的值设置为空字符串。因此,如果实际提交了输入但值为空,则此操作将失败。

51        if(part != null) {
52            fileUpload.setTransient(true);
53            fileUpload.setSubmittedValue(new NativeUploadedFile(part));
54        }
55        else {
56            fileUpload.setSubmittedValue("");
57        }

CommonsFileUploadDecoder#decodeSimple() 将在提交的文件名为空时将提交的值设置为空字符串,完全符合本意。

54        if(file != null) {
55            if(file.getName().equals("")) {
56                fileUpload.setSubmittedValue("");
57            } else {
58                fileUpload.setTransient(true);
59                fileUpload.setSubmittedValue(new DefaultUploadedFile(file));
60            }
61        }  

要让它工作,最好的办法是向 PrimeFaces 人员报告问题,同时自定义/修补渲染器,或切换回 Commons 上传器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 2011-12-13
    • 2013-01-10
    • 2015-08-05
    • 2012-05-28
    • 2016-02-16
    • 2012-11-08
    相关资源
    最近更新 更多