You need to sign in to do that
Don't have an account?
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;
}
}
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
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):
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
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;
}
Have you even tried to adapt the code posted?
At least you finally got it working, but have in mind that your statement
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.