Hi friends,
Recently i was working on a complex visual-force page, where page has lot of render and reRender.
My requirement is to make some fields mandatory on vf page but page as some javascript and reRender on save button which creates problem while i am making field mandatory using required attribute of <apex:inputfield> tag.
So to solve this problem i have maked field required by using custom controller and field having same look and feel as standard required attribute because i am using same standard CSS which standard inputfield is used on required field attribute.
Here the VF page and controller:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page Controller="RequiredFieldController" tabStyle="Account"> | |
<apex:form> | |
<apex:pageblock title="Required Example"> | |
<apex:pageblocksection> | |
<!-- Standard required attribute of inputfield --> | |
<apex:inputField value="{!acc.Name}" required="true"/> | |
<!-- Required field using controller --> | |
<apex:pageBlockSectionItem > | |
<apex:outputLabel value="Website"/> | |
<apex:outputPanel layout="block" styleClass="requiredInput"> | |
<apex:outputpanel layout="block" styleClass="requiredBlock"></apex:outputpanel> | |
<apex:inputField value="{!acc.Website}" /> | |
</apex:outputPanel> | |
</apex:pageBlockSectionItem> | |
<apex:inputfield value="{!acc.Phone}"/> | |
<apex:inputfield value="{!acc.Fax}"/> | |
</apex:pageblocksection> | |
<apex:pageBlockButtons location="bottom"> | |
<apex:commandButton value="Save" action="{!SaveAccount}"/> | |
</apex:pageBlockButtons> | |
</apex:pageblock> | |
</apex:form> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RequiredFieldController{ | |
public Account acc{get;set;} | |
public RequiredFieldController(){ | |
acc = new Account(); | |
} | |
public PageReference SaveAccount(){ | |
// validation for required fields. | |
if(acc.Website == null){ | |
acc.Website.addError('Please Enter Account Website.'); | |
return null; | |
} | |
insert acc; | |
return new PageReference('/' + acc.Id); | |
} | |
} |
Screen Shoots:-
Cheers!!!
Anurag Jain