简体   繁体   English

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

[英]FileUpload required=“true” doesn't work in PrimeFaces

I'm trying to put validation in my <p:fileUpload> . 我正在尝试将验证放入我的<p:fileUpload> when the user upload without putting any file he get's an error message. 当用户上传而没有放置任何文件时,他得到一条错误消息。 I'm using mode="simple" and required="true" but required="true" doesn't work. 我正在使用mode="simple"required="true"required="true"无效。

PS: I need to use mode="simple" because I need <p:commandButton> to submit other data. PS:我需要使用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>

As per the 5.2 source code, this will indeed fail when Servlet 3.0 native upload is being used instead of Apache Commons FileUpload. 根据5.2源代码,当使用Servlet 3.0本机上传而不是Apache Commons FileUpload时,这确实会失败。

The NativeFileUploadDecoder#decodeSimple() will only set the submitted value to an empty string when the input was not submitted at all. 仅在根本不提交输入时, NativeFileUploadDecoder#decodeSimple()才会将提交的值设置为空字符串。 This will thus fail if the input is actually submitted, but with an empty value. 因此,如果实际提交了输入,但值为空,则将失败。

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

The CommonsFileUploadDecoder#decodeSimple() will set the submitted value to an empty string when the submitted file name is empty, exactly as intented. 如果提交的文件名完全为空,则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        }  

Your best bet to get it to work is to report an issue to PrimeFaces guys and in the meanwhile either customize/patch the renderer, or switch back to Commons uploader. 使其生效的最佳选择是向PrimeFaces员工报告问题,同时自定义/修补渲染器,或切换回Commons上传器。

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

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