如何在p:fileUpload上进行验证 [英] How to put validation on p:fileUpload

查看:318
本文介绍了如何在p:fileUpload上进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与夫妇inputText一起,我在页面上拥有的强制性组件之一是p:fileUpload.因此,当我单击提交"时,<p:message>会显示在具有require=true的组件上,但用户没有键入/选择

Along with couples inputText, one of a mandatory component that I have on the page is a p:fileUpload. So when I click submit, <p:message> show up on component that have require=true, but the user did not type/select

我希望红色框Required也出现在上载组件旁边.这是我尝试过的.

I want the red box Required also appear next to the upload component. Here is what I have tried.

1.当我在p:fileUpload中设置required="true"时,什么也没有发生(不确定是否是错误).
2.我在p:fileUpload中放入了validator,下面是我的验证器来源

1 . when I set required="true" in p:fileUpload, nothing really happen (not sure if this is a bug).
2 . I put validator in p:fileUpload, below is my validator sources

public void validateFileUpload(FacesContext context, UIComponent component,
       Object value) throws ValidatorException {
   if(value == null){
     FacesMessage message = new FacesMessage();
     message.setSeverity(FacesMessage.SEVERITY_ERROR);
     message.setSummary("Error");
     message.setDetail("Required");
     throw new ValidatorException(message);      
   }
}

当我单击提交"时,什么都没有发生,即使我上载时也没有,validateFileUpload根本没有被调用(不确定这是否是错误)

nothing really happen when I click submit, not even when I go through the upload, validateFileUpload did not get called at all (not sure if this is a bug)

3.当我单击提交"时,如果其他所有事情都通过了,并且进入了我的操作方法,则可以检查该文件是否为空,然后返回FacesMessage并让p:growl进行拾取.但是,我不喜欢这种方式,因为它给用户带来了多层验证的感觉.

3 . When I click submit, if everything else pass, and I get into my action method, I am able to check if the file is null or not, then return a FacesMessage and let p:growl pick it up. However, I dont like it that way since it give the user a feeling of multiple layer of validation.

是否可以对p:fileUpload 进行更好的验证?

推荐答案

对于那些同样的问题,我在创建向导时遇到了这个问题.我使用的解决方法是将上传的文件存储在我的viewscoped bean的字段中,并在尝试导航到下一步时检查此字段.

For those with the same problem, I ran into this problem while creating a wizard. The workaround I used was to store the uploaded file in a field of my viewscoped bean and check this field when trying to navigate to the next step.

向导标签:

<p:wizard id="importBankAccountLogWizard"
            widgetVar="importBankAccountLogWizard"
            flowListener="#{bankAccountLogImportBean.onFlowProcess}">

文件上传标签(我已经设置了render和update属性,以便在第一次上传后显示一条消息,并且隐藏上载的内容):

File upload tag (I have the rendered and the update attribute set up so that a message will be shown and the uploaded will be hidden after the first upload):

<p:fileUpload id="bankAccountLogFileInput"
                                      fileUploadListener="#{bankAccountLogImportBean.setBankAccountLogFile}"  
                                      rendered="#{bankAccountLogImportBean.renderFileUploadInput}"
                                      mode="advanced"  
                                      update="importBankAccountLogWizard"  
                                      auto="true"
                                      sizeLimit="1000000" />

Bean:

public void setBankAccountLogFile(FileUploadEvent event)
{
    importFile = event.getFile();
    FacesMessage msg = new FacesMessage(Localization.g("FILE_HAS_BEEN_UPLOADED", event.getFile().getFileName()));
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public String onFlowProcess(FlowEvent event)
{
    if("bankAccountLogImportInputTab".equals(event.getOldStep()) &&
       importFile == null)
    {
        FacesMessage msg = new FacesMessage(Localization.g("UPLOAD_A_FILE_TO_CONTINUE"));
        FacesContext.getCurrentInstance().addMessage(null, msg);
        return event.getOldStep();
    }

    return event.getNewStep();
}

这篇关于如何在p:fileUpload上进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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