[SalesForce] Display all error messages

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;
          } 

Best Answer

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.