首页 文章

FileUpload required =“true”在PrimeFaces中不起作用

提问于
浏览
1

我正在尝试将验证放在我的 <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>

1 回答

  • 0

    根据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上传器 .

相关问题