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

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

问题描述

除了一对 inputText,我在页面上的一个强制性组件是 p:fileUpload.因此,当我单击提交时, 显示在具有 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 .我把 validator 放在 p:fileUpload 中,下面是我的验证器来源

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

文件上传标签(我设置了渲染和更新属性,以便在第一次上传后显示一条消息并隐藏上传的文件):

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

豆子:

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天全站免登陆