3

Visualforce page is displaying one error message at a time not all error messages if validation is failed for all fields.Below is my code in the controller.I have apex:pagemessages in visualforce page is display error messages

if((newFR.Fleet_Vehicle_Maintained__c == 'No')&& (newFR.Fleet_Explanation__c == NULL )) {
          newFR.Fleet_Explanation__c.addError('If the fleet vehicle is not being maintained by the rep, an explanation is required.');
                        return null;
          }
         if((newFR.Rep_Policy__c == 'No')&& (newFR.Not_Policy_Explanation__c == NULL )) {
           newFR.Not_Policy_Explanation__c.addError('Not_Following_Policy_Explanation__c');

          return null;
          } 
          if((newFR.FR_Status__c == 'Send for Representative')&& (newFR.Review_Date__c > system.today() )) {
           newFR.Review_Date__c.addError('Review Date cannot be a future date when sending  for acknowledgement.');

          return null;
          } 
  • which VF message tag are you using? – Chris Duncombe Oct 31 '13 at 18:21
  • apex:pagemessages – sfdc Oct 31 '13 at 18:22
6

The code you've produced is quitting after the first error (return null). A more usual scenario is to set a flag when validation fails, then check for the flag at the end of the validation to abort processing.

Here's a common example:

public PageReference doSomething() {
    Boolean okayToSave = true;
    if( condition1 ) {
        record.field1__c.addError(...);
        okayToSave = false;
    }
    if( condition2 ) {
        record.field2__c.addError(...);
        okayToSave = false;
    }
    if( conditionN ) {
        record.fieldN__c.addError(...);
        okayToSave = false;
    }
    if( !okayToSave ) {
        return null;
    }
    // Now save the data
    insert record;
    return Page.SuccessfulSave;
}

Of course, I left out the part where you use a try-catch block or other error handling for the actual save, because it's not relevant to what I'm trying to explain.

2

You are using the addError field which relates to the <apex:message> and <apex:messages> tags. If you want use your existing code then you would need to use somethign like this

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'This is apex:pageMessages'));

or you could change your vf tag to <apex:messages> and it should work with your apex code.

Jesse Altman has an excellent answer on here for this very subject. I would take a look at it. It really helped me to understand the difference. This explanation is the only place I have seen them all compared in one place and is an excellent resource.

Difference between the multiple messaging options in Visualforce?

  • Are you sure that's the case? I thought addError shows up in pageMessages, next to inputField if adding error to a field, and in message component for an inputField. The messages component is the same as pagemessages just without formatting. (At least that's my understanding). – Ralph Callaway Oct 31 '13 at 18:51
  • I went through the post but I have question.adderror method points to the field just like validation error on the field whereas apexpages.addmessage doesn't.Is there a way to use apexpages.addmessage to point to the field – sfdc Oct 31 '13 at 18:54
  • adderror shows up in pagemessages but not all error messages at a time – sfdc Oct 31 '13 at 18:55
  • Didn't you notice how he's aborting after the first error? That's why only one shows up. – sfdcfox Oct 31 '13 at 19:02
  • lol....wow. No i didn't. Yikes. Good catch – Chris Duncombe Oct 31 '13 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.