الخميس، 9 أبريل 2020

ADF input file validation

ADF input file validation

af:inpitFile component does not have validation support to restrict the type of file user can upload. We need to restrict the file type either in javascript or in backing bean. In this example, I have described how to restrict certain file types in input file component. it allows only excel file formats by checking the uploaded file type is either Xls or xlsx format.

ADF input file validation – Allows only Excel file

Created a JSF page with <af:inputFile> and <af:button> component. The value of inputText is bound with the backing bean, so we can get the file in baking bean when the submit button got clicked.Also note the property usesUpload=”true’ in <form>.
The button has actionListener and partialTrigger enabled. When the button clicked, it calls the uploadFile() method in backing bean
      <af:form  id="f1" binding="#{backingBeanScope.backing_index.f1}" usesUpload="true">

            <af:inputFile label="Upload Excel" id="if1" value="#{backingBeanScope.backing_index.file}"/>
            <af:button text="Submit" partialSubmit="true" actionListener="#{backingBeanScope.backing_index.uploadFile}" />

        </af:form>

Below is the backing bean class which read the file extension using the regular expression and validate the file type is xls or xlsx. If the validation fails then itthrows the error message using facesMessage.
package view.backing;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.component.rich.RichDocument;
import oracle.adf.view.rich.component.rich.RichForm;
import oracle.adf.view.rich.component.rich.input.RichInputFile;

import org.apache.myfaces.trinidad.model.UploadedFile;

public class Index {
    
    private UploadedFile file;

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

    public UploadedFile getFile() {
        return file;
    }
    
String getFileExtn(String filename){
        String parts[] = filename.split("\\.(?=[^\\.]+$)");
        return parts[1].toLowerCase();
    }
    
    boolean isExcel(String fileExtn){
        if(fileExtn.equals("xls") || fileExtn.equals("xlsx"))
            return true;
        else 
            return false;
        
    }
    
    
    public void uploadFile(ActionEvent actionEvent) {
        
        UploadedFile file = getFile();
        
        String fileName = file.getFilename();
        
        String fileExtn = getFileExtn(fileName);
        
        if(!isExcel(fileExtn)){
            
            FacesMessage Message = new FacesMessage("Not an excel file!");   
            Message.setSeverity(FacesMessage.SEVERITY_ERROR);   
            FacesContext.getCurrentInstance().addMessage(null, Message);   
        }
        else{
            //Process Logic
        }
        
    }
}


When we run the application in WebLogic, the page opens in the browser like below. I tried to upload an image file first and hits the submit button. The backing bean validates and throws an error message.

When I uploaded an excel file and hit the submit button, its do nothing.

ليست هناك تعليقات:

إرسال تعليق

ADF: Programmatic View Object Using Ref Cursor.

ADF: Programmatic View Object Using Ref Cursor. Posted by:  Manish Pandey   April 25, 2013   in  ADF   Leave a comment   3758 Views Sometime...