简体   繁体   中英

How to put validation on p:fileUpload

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

在此处输入图片说明

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

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

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

Is there a way to do better validation on p:fileUpload ?

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.

Wizard tag:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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