-1

I have a form in my Visualforce page with an inputFile tag. If the user clicks the submit button without choosing a file i want to display an error message and force him to choose a file from his computer. and if he choose a file the action will be processed and he will be redirected to another Visualforce page.

This the code of my visualforce page:

<apex:form id="myForm" >
        <apex:pageBlock >
            <apex:pageMessages />
                <apex:pageBlockButtons >
                    <apex:commandButton value="Send" action="{!processForm}"/>
             </apex:pageBlockButtons>
            <apex:pageBlockSection title="Form Data" columns="1">                
                <apex:inputText label="Name" value="{!name}"/>
                <apex:inputText label="Comment" value="{!myComment}"/>              
                <apex:inputFile value="{!myFile}" fileName="{!fileName}"/> 
            </apex:pageBlockSection>         
        </apex:pageBlock>
</apex:form> 

an this is the Apex code that process the form:

public PageReference processForm() {    
    String theFile = myFile.toString(); 
    // here i process the data of my form 

    // and here i redirect to a new page
    PageReference newPage = new PageReference(url to my page);  
    newPage.setRedirect(true);

    return newPage; 
}

3 Answers 3

3

First you have to add <apex:pageMessages /> Tag after <apex:pageBlock > and then in your Apex code add this code:

if(myFile == null) {
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please upload your file'));
    return ApexPages.CurrentPage();
} else {
    String theFile = myFile.toString(); 
    // here i process the data of my form 

    // and here i redirect to a new page
    PageReference newPage = new PageReference(url to my page);  
    newPage.setRedirect(true);

    return newPage; 
}

With this code it should return an error message if the user doesn't upload the file, otherwise it will process your form.

1

Here is a example on how to do form validation with Javascript in visualforce. Hope this will help.

<apex:page controller="clssfpoc">
  <apex:pageBlock >
    <apex:form >
      <p>
        <apex:outputLabel value="Father"/>
      </p>
      <apex:inputText value="{!Father}" id="fName" />
      <p>
        <apex:outputLabel value="Guardian"/>
      </p>
      <apex:inputText value="{!Guardian}" id="gName" />
      <p>
        <apex:commandButton value="Save" onclick="return validate()" />
      </p>
        <script language="javascript">
            function validate() {
                try {
                    var fNameObj = document.getElementById("{!$Component.fName}");
                    var gNameObj = document.getElementById("{!$Component.gName}");
                    if (trim(fNameObj.value) == "" && trim(gNameObj.value) == "") {
                        alert("Father/Guardian is mandatory!");
                        return false;
                    }
                    return true;
                } catch (e) {
                    alert(e);
                    return false;
                }
            }
        </script>
    </apex:form>
  </apex:pageBlock>
</apex:page>

Updae

I have updated your code to incorporate the above solution. It is working. You can extend based on your requirement.Hope this helps.

VF Page

<apex:page controller="FormValidate">
    <style type="text/css">
    .redFont{
        font-style: bold;
        color: red;
    }
    </style>
    <apex:form id="myForm" >
            <apex:pageBlock id="thPageBlock" >
                <apex:pageMessages/>
                 <apex:pageBlockButtons  >
                    <apex:commandButton value="Send" action="{!processForm}" onclick="return validateForm();"/>
                 </apex:pageBlockButtons>
                <apex:pageBlockSection title="Form Data" columns="1" id="thePageBlockSection">                
                    <apex:inputText label="Name" value="{!name}"/>
                    <apex:inputText label="Comment" value="{!myComment}"/>              
                    <apex:inputFile value="{!attach.body}" fileName="{!attach.name}" id="fName"/><apex:outputLabel styleClass="redFont" id="errMsg"></apex:outputLabel>
                </apex:pageBlockSection>         
            </apex:pageBlock>
            <script type="text/javascript">
                function validateForm() {
                    try {
                        var fNameObj = document.getElementById("{!$Component.myForm.thPageBlock.thePageBlockSection.fName}");
                        var errObj = document.getElementById("{!$Component.myForm.thPageBlock.thePageBlockSection.errMsg}");
                        if (fNameObj.value.length == 0) {
                            errObj.innerHTML = "Please select a file before sending!!!"
                            return false;
                        }
                        return true;
                    } catch (e) {
                        alert(e);
                        return false;
                    }
                }           
            </script>
    </apex:form> 
</apex:page>

Controller

public with sharing class FormValidate {
    public String name{get; set;}
    public String myComment{get; set;}
    public String fileName{get; set;}
    public Attachment attach {get;set;}

    public FormValidate() {

    }
    public PageReference processForm() {    
        // here i process the data of my form 

        // and here i redirect to a new page
        PageReference newPage = new PageReference('/');  
        newPage.setRedirect(true);

        return newPage; 
    }
}
0

You can use Required Attribute in your Apex:InputFile. This will show error massage if no file selected.

1
  • Required attribute doesn't work with Apex:InputFile Tag
    – ihssan
    Aug 26, 2015 at 15:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .