PrimeFaces< p:fileUpload mode =“advanced”>>验证器没有被解雇 [英] PrimeFaces <p:fileUpload mode="advanced"> validator not fired
问题描述
$ pre $ @ $ $ $ $ $ $ $ $ $公共类FileLimitValidator实现验证{
@Override
public void validate(final FacesContext context,final UIComponent component,
final Object value)throws ValidatorException {
final String fileLimit =(String)component 。.getAttributes()获得( fileLimit);
final String size =(String)component.getAttributes()。get(size); (fileLimit!= null&& size!= null){
if(Integer.valueOf(size)> = Integer.valueOf(fileLimit)){$ b $(
) b FacesUtils.throwErrorExceptionFromComponent(组件,fichero_confidencialidad_error);
和在我facelet我已经试过:
< p:fileUpload id =#{id} FileUpload
fileUploadListener = #{豆[metodoTratarFichero]} 模式= 先进
倍数= 真 allowTypes = #{allowTypes} showButtons = 假
更新=#{ID} ListaDocs #{file}} {#{id} MsgErrorauto =true
label =#{fileuploadmsg.label_boton}
invalidFileMessage =#{fileuploadmsg.tipo_incorrecto}>
< / p:fileUpload>
和:
< p:fileUpload id =#{id} FileUpload
fileUploadListener =#{bean [metodoTratarFichero]}mode =advanced
multiple =trueallowTypes = #{allowTypes}showButtons =false
update =#{id} ListaDocs#{id} MsgErrorauto =true
label =#{fileuploadmsg.label_boton}
invalidFileMessage =#{fileuploadmsg.tipo_incorrecto}
validator =fileLimitValidator>
< / p:fileUpload>
和:
< p:fileUpload id =#{id} FileUpload
fileUploadListener =#{bean [metodoTratarFichero]}mode =advanced
multiple =trueallowTypes = #{allowTypes}showButtons =false
update =#{id} ListaDocs#{id} MsgErrorauto =true
label =#{fileuploadmsg.label_boton}
invalidFileMessage =#{fileuploadmsg.tipo_incorrecto}
validator =#{fileLimitValidator}>
< / p:fileUpload>
和:
< p:fileUpload id =#{id} FileUpload
fileUploadListener =#{bean [metodoTratarFichero]}mode =advanced
multiple =trueallowTypes = #{allowTypes}showButtons =false
update =#{id} ListaDocs#{id} MsgErrorauto =true
label =#{fileuploadmsg.label_boton}
invalidFileMessage =#{fileuploadmsg.tipo_incorrecto}
validator =#{fileLimitValidator.validate}>
< / p:fileUpload>
但是不会调用validate方法。什么是正确的方法来做到这一点?
根据 FileUpload
和 FileUploadRenderer
源代码,只有在使用 mode =simple
时才会调用验证器(注意:这又要求 ajax =false
on命令)。高级模式将不会将上传的文件设置为组件的提交值,导致它保持 null
直到侦听器方法被调用。只要提交的值是 null
,就不会调用校验器。
我不知道这个是故意的。理论上,应该可以将 UploadedFile
设置为提交的值,并让验证器依赖它。您可能需要在 PrimeFaces问题跟踪器上创建增强报告。
与此同时,尽管它是一个 poor practice ,最好的办法是在 fileUploadListener
方法中执行验证。您可以通过 FacesContext
触发验证失败添加消息,如下所示:
if(fail){
它在
context.validationFailed();
context.addMessage(event.getComponent()。getClientId(context),new FacesMessage(
FacesMessage.SEVERITY_ERROR,messageSummary,messageDetail));
$ b否则,你需要为<$ c创建一个自定义的渲染器$ c>< p:fileUpload>
decode()
中设置提交的值(但是我不能保证它可以工作在实践中,你可能会碰到一个奇怪的问题,这可能会成为PrimeFaces最初没有这样做的原因。)
顺便说一句,你的第一个和第二个验证器的尝试是正确的。第三次尝试仅在您使用
@ManagedBean
而不是@FacesValidator
(which往往是做时的@EJB
是强制性的注射—这在@FacesValidator
中是不可能的)。第四次尝试是无效的。since fileLimit doesn't exist in primefaces 3.4 anymore I'm trying a work around implementing a validator, the problem is that the method validate is never invoked. That's my Validator:
@FacesValidator(value ="fileLimitValidator") public class FileLimitValidator implements Validator { @Override public void validate(final FacesContext context, final UIComponent component, final Object value) throws ValidatorException { final String fileLimit = (String)component.getAttributes().get("fileLimit"); final String size = (String)component.getAttributes().get("size"); if (fileLimit!=null && size!=null) { if (Integer.valueOf(size) >= Integer.valueOf(fileLimit)) { FacesUtils.throwErrorExceptionFromComponent(component,"fichero_confidencialidad_error"); } } } }
and in my facelet I've tried:
<p:fileUpload id="#{id}FileUpload" fileUploadListener="#{bean[metodoTratarFichero]}" mode="advanced" multiple="true" allowTypes="#{allowTypes}" showButtons="false" update="#{id}ListaDocs #{id}MsgError" auto="true" label="#{fileuploadmsg.label_boton}" invalidFileMessage="#{fileuploadmsg.tipo_incorrecto}" > <f:validator validatorId="fileLimitValidator"/> <f:attribute name="fileLimit" value="#{fileLimit}"/> <f:attribute name="size" value="#{listaDocumentos.size}"/> </p:fileUpload>
and:
<p:fileUpload id="#{id}FileUpload" fileUploadListener="#{bean[metodoTratarFichero]}" mode="advanced" multiple="true" allowTypes="#{allowTypes}" showButtons="false" update="#{id}ListaDocs #{id}MsgError" auto="true" label="#{fileuploadmsg.label_boton}" invalidFileMessage="#{fileuploadmsg.tipo_incorrecto}" validator="fileLimitValidator"> <f:attribute name="fileLimit" value="#{fileLimit}"/> <f:attribute name="size" value="#{listaDocumentos.size}"/> </p:fileUpload>
and:
<p:fileUpload id="#{id}FileUpload" fileUploadListener="#{bean[metodoTratarFichero]}" mode="advanced" multiple="true" allowTypes="#{allowTypes}" showButtons="false" update="#{id}ListaDocs #{id}MsgError" auto="true" label="#{fileuploadmsg.label_boton}" invalidFileMessage="#{fileuploadmsg.tipo_incorrecto}" validator="#{fileLimitValidator}"> <f:attribute name="fileLimit" value="#{fileLimit}"/> <f:attribute name="size" value="#{listaDocumentos.size}"/> </p:fileUpload>
and:
<p:fileUpload id="#{id}FileUpload" fileUploadListener="#{bean[metodoTratarFichero]}" mode="advanced" multiple="true" allowTypes="#{allowTypes}" showButtons="false" update="#{id}ListaDocs #{id}MsgError" auto="true" label="#{fileuploadmsg.label_boton}" invalidFileMessage="#{fileuploadmsg.tipo_incorrecto}" validator="#{fileLimitValidator.validate}"> <f:attribute name="fileLimit" value="#{fileLimit}"/> <f:attribute name="size" value="#{listaDocumentos.size}"/> </p:fileUpload>
but the validate method is never called. What is the correct way to do it?
解决方案According to the
FileUpload
andFileUploadRenderer
source code, the validator is only invoked whenmode="simple"
is been used (note: this in turn requiresajax="false"
on command). The advanced mode will namely not set the uploaded file as component's submitted value, causing it to remainnull
until the listener method is invoked. As long as the submitted value isnull
, the validators are not invoked.I'm not sure if this is intentional. Theoretically, it should be possible to set
UploadedFile
as submitted value and have the validator to rely on it. You might want to create an enhancement report at PrimeFaces issue tracker.In the meanwhile, in spite of it being a poor practice, your best bet is really performing the validation in
fileUploadListener
method. You can just trigger validation failure add faces messages through theFacesContext
like follows:if (fail) { context.validationFailed(); context.addMessage(event.getComponent().getClientId(context), new FacesMessage( FacesMessage.SEVERITY_ERROR, messageSummary, messageDetail)); }
Otherwise, you'd need to create a custom renderer for the
<p:fileUpload>
which sets the submitted value during thedecode()
(I however don't guarantee that it would work in practice, you'll maybe stumble upon a peculiar problem which may turn out to be the reason why PrimeFaces didn't initially implement it like that).By the way, your first and second validator attempt are correct. The third attempt works only if you used
@ManagedBean
instead of@FacesValidator
(which is often done when injection of an@EJB
is mandatory — which isn't possible in a@FacesValidator
). The fourth attempt is invalid.这篇关于PrimeFaces< p:fileUpload mode =“advanced”>>验证器没有被解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!