我正在尝试将验证放在我的 <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 回答
根据5.2源代码,当使用Servlet 3.0本机上载而不是Apache Commons FileUpload时,这确实会失败 .
NativeFileUploadDecoder#decodeSimple()仅在输入未提交时才将提交的值设置为空字符串 . 如果实际提交了输入,但是输入值为空,则会失败 .
当提交的文件名为空时,CommonsFileUploadDecoder#decodeSimple()会将提交的值设置为空字符串,与预期的完全一样 .
让它发挥作用的最佳选择是向PrimeFaces人员报告问题,同时自定义/修补渲染器,或切换回Commons上传器 .