Pages

Monday, September 22, 2008

How do custom validations in Visualforce Pages?

Implementing validation in Visual Force pages can be tricky as times because individual field support a "required" behavior while you may need something something much more complex than that.

In fact in order for you to be able to validate the data you may need to put of a whole block of code to support it.

Here is how you can add data validation into your Visual Force pages:

1) In order to be able to support this requirement you either need to create a new Controller or extend an existing one.

2) Find out on what events (button click, etc) you need to carry out validation.

3) Write an Apex method for your controller "IsValid" which returns a boolean value indicating whether the validation was successful or not.

4) Put your validation logic in that method and call this method in the chosen events such as button clicks, etc.

Below is as example you can easily test on your own SalesForce Developer portal and learn more:


In this example, only when user selects the last value of the list is not required to provide "Comments" otherwise "Comments" are mandatory!



Visual Force Page Markup Code:


<apex:page controller="ValidationFormPageController" tabStyle="Account">
<apex:sectionHeader title="Validation Test Page"></apex:sectionHeader>
<apex:form >
<apex:pageBlock title="" id="pageBlock">
<apex:pageBlockButtons location="top">
<apex:outputLabel value="Options: " for="chooseView"></apex:outputLabel>
<apex:selectList id="chooseView" value="{!selectedValue}" size="1">
<apex:selectOption itemValue="Value1" itemLabel="Label Value 1"></apex:selectOption>
<apex:selectOption itemValue="Value2" itemLabel="Label Value 2"></apex:selectOption>
<apex:selectOption itemValue="Value3" itemLabel="Label Value 3"></apex:selectOption>
</apex:selectList><br/>
<apex:outputLabel value="Comments: " for="theTextArea"></apex:outputLabel>
<apex:inputTextarea id="theTextArea" value="{!inputTextValue}" style="{!textAreaStyle}" ></apex:inputTextarea><br/><br/>
<apex:commandButton value="Run" action="{!Run}" id="theButton" rerender="pageBlock"></apex:commandButton>
</apex:pageBlockButtons>
<apex:pageMessages ></apex:pageMessages>
</apex:pageBlock>
</apex:form>
</apex:page>






Visual Force Page Markup Code:


public class ValidationFormPageController {

private string textAreaStyle = '';

private string selected_Value = 'Value1';

private string inputTextValue = '';

public string getSelectedValue()

{

return selected_Value;

}

public void setSelectedValue(String value)

{

selected_Value = value;

}

public string getInputTextValue()

{

return inputTextValue;

}

public void setInputTextValue(string Value)

{

inputTextValue = Value;

}

public string getTextAreaStyle()

{

return textAreaStyle ;

}

public PageReference Run() {

if (!isValid())

{

//what to show when the data is not valid:

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Validation Failed: Comments are required.'));

textAreaStyle = 'border-size:2px; border-color:red;border-style:solid;';

return null;

}

else

{

// things to do when data is valid.

textAreaStyle = '';

}

//The rest of your logic

return null;

}

private Boolean isValid()

{

// this method is called to validate the data entered by user.

if (selected_Value != 'Value3')

{

if (inputTextValue == '')

return false;

else

return true;

}

else

return true;

}

}




8 comments:

  1. well this is what u put validation..but can it possible if we have some required field but in case we want to come out without saving the required field..

    say for example when u trying to convert lead when u convert it in existing account then it navigate to other page (for creating new contact or attach existing contact) without saving task's required field.

    i m stuck with this issue even i click cancel button i cant navigate to other page it will shows error like u must have to enter data

    please if u have solution then inform me
    my mail address is amar_4chat@yahoo.co.in

    if u need my coding then please mail me

    please help me out with any workaround

    ReplyDelete
  2. Thanks for your post you have really helped me with my project

    ReplyDelete
  3. Many institutions limit access to their online information. Making this information available will be an asset to all.

    ReplyDelete
  4. thanks a million!!! saved me few hours!

    ReplyDelete
  5. thanks u saved a lot ...with respect to time

    ReplyDelete
  6. can u plz tell me how we validate a textbox(i don't want a numeric value in text box.)plz tell me validation code..

    regard
    Madan

    ReplyDelete
  7. Excellent! Thanks so much, this is very helpful.

    Is there a way to have a pageMessages in multiple locations on the visualforce page for many validation rules?

    ReplyDelete
  8. in salesforce for predefined objects provided validation rules so is there any alternative providing validation for custom objects without writing code in controller

    ReplyDelete