ADF 11g – Validation of Uploaded Files with the inputFile component

 

Today we had a little issue with the inputFile component in ADF 11g, especially with its behavior after failed validation. Our situation: the inputFile component has autoSubmit set to true – so immediately after selecting a file in the browser dialog is the upload started in an asynchronous request. Validation is performed, either from validators or in a valueChangeListener (because I was too lazy to look up the syntax for a validator).

The behavior of the inputFile component is such that after the initial upload, the name of the uploaded file is shown as a read only value and the browse button is replaced by an update button: we can change the file, but not perform the initial upload anymore. When the user presses the update button, a small pop up appears in which we can browse for a file, click on the OK button to have it uploaded or click on Cancel to have the update aborted.

ADF 11g - Validation of Uploaded Files with the inputFile component inputFileValidation2

Unfortunately, when the file that was initially uploaded has failed validation, the OK button in this popup does not allow us to actually upload the newly selected file. The page continues to display the uploaded, invalid file and the update of that file to resolve the validation errors can not be performed.

We solved this issue

using the resetValue() method on the RichInputFile component: when we found that the uploaded file did not meet the validation rules, we add the FacesMessage to indicate the error, set the component to be invalid and we reset its value. Now the component is not changed to the readonly file name with update button, but instead continues to dispay as an input field with a browse button.

ADF 11g - Validation of Uploaded Files with the inputFile component inputFileValidation

We feel this approach is justified as after uploading a file that does not pass validation, there is nothing the user can do to overcome that failure.

The code of the page and the bean

The page with the inputFile component:

    <af:document id="d1">
      <af:form id="f1" usesUpload="true">
        <af:panelHeader text="File Uploader" id="ph1">
          <af:inputText label="Value (String)" id="it1"
                        value="#{fileBean.someStringValue}"/>
          <af:inputFile label="File to upload" id="if1" autoSubmit="true"                          valueChangeListener="#{fileBean.fileUpdate}"  
                        value="#{fileBean.file}" partialTriggers="if1"  />
        </af:panelHeader>
        <af:commandButton text="Save" id="cb1"/>
      </af:form>
    </af:document>

The managed bean that holds the values and performs validation:

public class FileBean {

    private String       someStringValue;
    private UploadedFile file;
    private UIComponent inputFile;


    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setInputFile(UIComponent inputFile) {
        this.inputFile = inputFile;
    }

    public UIComponent getInputFile() {
        return inputFile;
    }

    public void setSomeStringValue(String someStringValue) {
        this.someStringValue = someStringValue;
    }

    public String getSomeStringValue() {
        return someStringValue;
    }

    public void fileUpdate(ValueChangeEvent valueChangeEvent) {
        RichInputFile inputFileComponent = (RichInputFile)valueChangeEvent.getComponent();
        UploadedFile newFile = (UploadedFile)valueChangeEvent.getNewValue();
        if (newFile.getFilename().endsWith("gif")) {
          FacesContext.getCurrentInstance().addMessage( inputFileComponent.getClientId(FacesContext.getCurrentInstance())
                                                      , new FacesMessage(FacesMessage.SEVERITY_ERROR
                                                                         , "GIF files are not allowed"
                                                                         , "This file ("+ newFile.getFilename()+") is not allowed; the extension gif is not allowed."
                                                                         )
                                                      );
          inputFileComponent.resetValue();
          inputFileComponent.setValid(false);
        }
    }
}

Resources

JDeveloper 11g project with the sources: InputFileValidation.zip.

2 Comments

  1. muhamamd arshad February 20, 2012
  2. Alberto Tapia December 29, 2010