PrimeFaces <p:fileUpload mode="高级">验证器没有被解雇 [英] PrimeFaces &lt;p:fileUpload mode=&quot;advanced&quot;&gt; validator not fired

查看:30
本文介绍了PrimeFaces <p:fileUpload mode="高级">验证器没有被解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于fileLimit 不再存在于primefaces 3.4 中,我正在尝试解决实现验证器的问题,问题是从未调用过validate 方法.那是我的验证器:

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");
            }
        }
    }
}

并在我的 facelet 中尝试过:

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>

和:

    <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>

和:

    <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>

和:

    <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?

推荐答案

根据FileUploadFileUploadRenderer 源代码,验证器仅在使用了 mode="simple"(注意:这反过来需要 ajax="false" on command).高级模式不会将上传的文件设置为组件的提交值,导致它保持 null 直到调用侦听器方法.只要提交的值为 null,验证器就不会被调用.

According to the FileUpload and FileUploadRenderer source code, the validator is only invoked when mode="simple" is been used (note: this in turn requires ajax="false" on command). The advanced mode will namely not set the uploaded file as component's submitted value, causing it to remain null until the listener method is invoked. As long as the submitted value is null, the validators are not invoked.

我不确定这是不是故意的.理论上,应该可以将 UploadedFile 设置为提交值并让验证器依赖它.您可能希望在 PrimeFaces 问题跟踪器上创建增强报告.

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.

与此同时,尽管它是一个穷人练习,最好的办法是在 fileUploadListener 方法中真正执行验证.您可以通过 FacesContext 触发验证失败添加人脸消息,如下所示:

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 the FacesContext like follows:

if (fail) {
    context.validationFailed();
    context.addMessage(event.getComponent().getClientId(context), new FacesMessage(
        FacesMessage.SEVERITY_ERROR, messageSummary, messageDetail));
}

否则,您需要为 <p:fileUpload> 创建一个自定义渲染器,它在 decode() 期间设置提交的值(但是我不不能保证它会在实践中起作用,您可能会偶然发现一个特殊的问题,这可能是 PrimeFaces 最初没有像那样实现它的原因).

Otherwise, you'd need to create a custom renderer for the <p:fileUpload> which sets the submitted value during the decode() (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).

顺便说一下,您的第一次和第二次验证器尝试是正确的.第三次尝试仅在您使用 @ManagedBean 而不是 @FacesValidator (当必须注入 @EJB 时通常会这样做 — 这在 @FacesValidator 中是不可能的).第四次尝试无效.

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="高级">验证器没有被解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆