function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pranav_VaidyaPranav_Vaidya 

Validating custom VF page input and show error message

Hi,

 

I am validating input from a custom VF Page. Everything works fine inclyding the validation function except the error message is not shown on the VF page when Controller class returns PageReference.

I am trying to do this -

  - Validate user input on click of Save button

  - If there is an errror stay on the same VF page with relevant error message shown

Any help is much appreciated. Thanks.

 

Below is controller class code-

public class CustomTripEntryController {
   private String stCurrentPageURL;
   public Leg__c CurrTripLeg {get; set;}
 
  public CustomTripEntryController(ApexPages.StandardSetController TripStdController){
    stCurrentPageURL = ApexPages.currentPage().getURL();
  }

  public pagereference InsertNewRecord(){
    if(CheckInput() == false){
      ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Validation Failed'));  --> Add error msg
      PageReference currPage = new PageReference(stCurrentPageURL); 
     pageref.setredirect(true);  --> Return page reference
     return currPage;
  } else{
      try{
          //Logic to insert new record
     } catch (exception e){
        //Error handler
    }
  }
}

private boolean CheckInput(){    -->Function to validate input
    boolean blIsInputValid = false;
    //Validate From_City
    system.debug('Validating From city');
    if(currTripLeg.From_City__c == null){
      system.debug('from City not found');
      blIsInputValid = false; }
    else {
       system.debug('From City found');
       blIsInputValid = true;
    }
    return blIsInputValid;
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Pranav_VaidyaPranav_Vaidya

I am able to resolve this now. I made a small change to my code as below and everything works as expected

 

Thanks.

 

 if(CheckInput() == false){
      ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Validation Failed'));  
      PageReference currPage = new PageReference(stCurrentPageURL); 
     return null;
  }

All Answers

SeAlVaSeAlVa

That kind of validation should be done on 'Validation Rules'. (if the logic is complex, then in triggers, but remember that Validation Rules don't need code coverage on tests).

 

Code that isnull logic in a Validation Rule and use the following code to catch the exception on your save method (controller): 

try{
  insert currTripLeg; // or the statement you want to try;
  return xyz; // whatever your pageReference for success page is.
}
catch(DmlException ex){
  ApexPages.addMessages(ex); // this will show the errors on the page
  return null; // returning null makes you stay on the same page
}

 Reference: http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_validation.htm

 

 

 

P.S. Take into account that all the logic that you program in your VF to validate something and is not in your Validation Rules or triggers can be skipped, so make sure you also do it in either triggers or VR

 

 

Pranav_VaidyaPranav_Vaidya

I am able to resolve this now. I made a small change to my code as below and everything works as expected

 

Thanks.

 

 if(CheckInput() == false){
      ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Validation Failed'));  
      PageReference currPage = new PageReference(stCurrentPageURL); 
     return null;
  }

This was selected as the best answer
SeAlVaSeAlVa

Have you even tried to adapt the code posted?

 

At least you finally got it working, but have in mind that  your statement 

PageReference currPage = new PageReference(stCurrentPageURL); 

is useless as you don't use currPage for nothing in there, you just return null.

 

Next time, try to read what other people replies. We take part of our valuable time to help other people like you, without asking anything in return (apart from a thank you and a 'mark as answer').

 

Regards.