2

I have multiple Visualforce pages with inputText that needs to be validated that:

  1. Correct datatype
  2. Correct length
  3. Regex (ssn, phone number)

What is the best way to do the validation?

  1. Custom validation class (Helper class)
  2. Validation rules
  3. Javascript validation

Any recommended validation classes that could be used?

5
  • for validation rules, check the SFDC doc developer.salesforce.com/docs/… and resources.docs.salesforce.com/200/7/en-us/sfdc/pdf/…
    – cropredy
    Feb 11, 2016 at 18:43
  • @crop1645 very familiar with validation rules. Looking more for best practices as if I have multiple fields I have to set validation rules for each field which doesn't make sense as allot of duplication. Also changes are not easy to manage as I have to change each field individually. Feb 11, 2016 at 18:51
  • ah, well, if using common validation logic, use a utility validation class unless you want to validate at browser level, then use jQuery or other JS solution - folks at stackoverflow can help on this approach
    – cropredy
    Feb 11, 2016 at 18:59
  • Helper classes for sure, easy for the most part and most importantly reuseable
    – EricSSH
    Feb 11, 2016 at 19:14
  • @EricSSH can you recommend some helper classes? Feb 11, 2016 at 19:26

2 Answers 2

2

So I'm posting this as an answer because I'm showing you code, but this is only one solution to many. This is meant for a custom controller that is handled by a button on your Visualforce page.. So say you have button Submit on your VF page, you would check what ever data before you save by using your getters and setters that is coming from your inputText. This was my solution which was a VF Page controlled by a custom controller. Granted you won't be using this exact code, this just may give you ideas to create your own

VF PAGE
<apex:pageMessages id="feedback" escape="false" />

public void prepareDataForSave()
{
    Boolean result = true;
    missingDataError = '';

    if(transportationWrapper.transportRequested)
    {
        if(transportationWrapper.type == 'None')
        {
            missingDataError += '<br />  ' + VEHICLE_TYPE_MUST_BE_SELECTED;
            result = false;
        }
    }

    if(specSelected)
    {
        if(tripWrapper.amenityAmmount.Budget__c <= 0)
        {
            missingDataError += '<br />  ' + BUDGET_IS_REQUIRED;
            result = false;
        }

        if(String.isBlank(specialAmenitiesWrapper.description))
        {
            missingDataError += '<br />  ' + DESCRIPTION_IS_REQUIRED;
            result = false;         
        }
    }

    if(maximumOccupancy != null)
    {
        if(numberofAdults + numberOfChildern > maximumOccupancy)
        {
            if(!tripWrapper.overOccupancyLimitConfirmation || tripWrapper.resolutionTextBlockArea == null || String.isBlank(tripWrapper.resolutionTextBlockArea))
            {
                missingDataError += '<br />  ' + OVER_OCCUPANCY_WARNING;
                result = false;   
            }
        }
    }
    if(contactPlannerSelected && unknownPlannerSelected)
    {
        missingDataError += '<br />  ' + ONLY_ON_PLANNER_CONTACT_CAN_BE_SELECTED;
        result = false;  
    }

    if(result)
    {
        Submit(result);
    }
    else
    {
        displayFeedback(ApexPages.Severity.Error, String.valueOf(ERROR_SAVING_DATA + missingDataError));
        missingDataError = '';
    }       
}

// User feedback
// ApexPages.Severity Reference: CONFIRM / ERROR / FATAL / INFO / WARNING
private void displayFeedback(ApexPages.Severity msgType, String message) 
{
    ApexPages.Message msg = new ApexPages.Message(msgType, message);
    ApexPages.addMessage(msg);
}
2

For valid email helper class method, here is an example

//  --------------------------------------------------------
//  isValidEmail - source: http://salesforcesource.blogspot.com/2010/01/utilizing-apex-pattern-and-matcher.html
//  --------------------------------------------------------
public static Boolean isValidEmail(String emailIn) {

  String emailRegex = '^[a-zA-Z0-9._|\\\\%#~`=?&/$^*!}{+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$'; // source: http://www.regular-expressions.info/email.html
  return String.isBlank(emailIn)
     ? false
     : Pattern.compile(emailRegex).matcher(emailIn).matches();
} 

You must log in to answer this question.

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