简体   繁体   English

JSF h:inputFile自定义验证器未调用

[英]JSF h:inputFile custom validator is not called

I'm building a custom valdiator to do some validations over an uploaded image so I did the following : 我正在构建一个自定义的valdiator,以对上传的图像进行一些验证,因此我执行了以下操作:

<h:form styleClass="form" role="form" prependId="false" enctype="multipart/form-data">  
          <h:inputFile  styleClass="form-control"  id="slideImage"> 
                 <f:validator validatorId="NewImageValidator"/>
          </h:inputFile>
// Rest of form data & components
</h:form>

NewImageValidator.java NewImageValidator.java

@FacesValidator(value = "NewImageValidator")
public class NewImageValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        System.out.println("NewImageValidator");

    }
}

The issue is that the validator is never called (NewImageValidator is never printed on output) inside the h:inputFile tried calling the validator inside an h:inputText and it worked just fine So what is the wrong with h:inputFile here 问题是,在h:inputFile内从未调用验证器(NewImageValidator从未在输出上打印),尝试在h:inputText内调用验证器,它工作得很好,所以h:inputFile了?

Thanks in advance 提前致谢

<h:inputFile  styleClass="form-control"  id="slideImage"> 
    <f:validator validatorId="NewImageValidator"/>
</h:inputFile>

First, you are- not using a value on the h:inputFile , hence your file will not be submitted into any BakingBean Property and the value passed to the converter will always be null . 首先,您没有在h:inputFile上使用value ,因此您的文件将不会提交到任何BakingBean属性中,并且传递给转换器的值将始终为null That's not true actually. 实际上那不是真的。 The validator will still pickup the right value, but ofc. 验证器仍会选择正确的值,但是ofc。 if you want continue to use the file, you should store it in your backing bean. 如果要继续使用该文件,则应将其存储在支持bean中。

Second, your form is missing a submit button. 其次,您的表单缺少提交按钮。 If you have a look at the JSF LifeCycle, the validation is invoked, after the values of the components have been evaluated (after submitting a form, not while the user fills out data, ie is picking a file) 如果您看一下JSF LifeCycle,则在评估了组件的值之后(在提交表单之后,而不是在用户填写数据即选择文件时),将调用验证。

If you want to execute the validation right away, there is no way around (partial)-submitting the form and transfer the file to the server. 如果要立即执行验证,则无法(部分)提交表单并将文件传输到服务器。 Depending on the size this might be a bad approach. 根据大小,这可能是一个不好的方法。

Bot for that purpose you can use f:ajax : 为此,您可以使用f:ajax

<h:form styleClass="form" role="form" prependId="false" enctype="multipart/form-data">  
    <h:inputFile value="#{myBean.uploadedFile}" styleClass="form-control"  id="slideImage"> 
        <f:validator validatorId="NewImageValidator"/>
        <f:ajax event="change" execute="slideImage" /> 
    </h:inputFile>
</h:form>

but i would prefer to only transfer files the user decides to submit (using a regular submit button). 但我希望仅传输用户决定提交的文件(使用常规的提交按钮)。 He might re-pick several files, and you don't need to upload them all as the user changes his mind. 他可能会重新选择几个文件,并且当用户改变主意时,您不需要全部上传它们。

I think there is no client-side validation of files, because your browser would not allow any client-side script to access an unsubmitted file on the filesystem. 我认为没有客户端验证文件,因为您的浏览器不允许任何客户端脚本访问文件系统上未提交的文件。

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

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