+ Start a Discussion
Alex Wong 4Alex Wong 4 

How to show several error message of validation rules in the same time?

My page is a form for information inserting. There are 4 fields for URL input and I want to check for their format. I wrote 4 validation rules in the custom object for each fields. However, the error message just show up one each time, even 4 of them are all wrong. How can I show all 4 in the same time when users type it wrong for all at the same time? 
Here is my apex code:
public class extattachfile {

Public attachment objAttachment{get;set;}
Public attachment objAttachment2{get; set;}
Public attachment objAttachment3{get; set;}
Public attachment objAttachmentt{get; set;}
Public attachment objAttachments{get; set;}
Public Artist__c artist{get; set;}

Public extattachfile(apexpages.standardcontroller stdCon) {
 
objAttachment = new Attachment();
objAttachment2 = new Attachment();
objAttachment3 = new Attachment();
objAttachmentt = new Attachment();
objAttachments = new Attachment();
artist= new Artist__c ();
    }

public PageReference save() {
        Boolean checkAttachment = false;
                    try{
    if(artist.Id == null){
        insert artist;
    }
}catch (DMLException e) {
    if(e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION')){
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter a valid URL.'));
    }else{
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,string.valueof(e)));
    }
    return null;
}


        List<Attachment> attachmentList = new List<Attachment>();
        if(objAttachment.Body != null){
            objAttachment.ParentId = artist.id;
            attachmentList.add(objAttachment);
            checkAttachment = true;
        }
        if(objAttachment2.Body != null){
            objAttachment2.ParentId = artist.id;
            attachmentList.add(objAttachment2);
            checkAttachment = true;
        }
        if(objAttachment3.Body != null){
            objAttachment3.ParentId = artist.id;
            attachmentList.add(objAttachment3);
            checkAttachment = true;
        }
          
List<Attachment> attachmentListother = new List<Attachment>();
        if(objAttachmentt.Body != null){
            objAttachmentt.ParentId = artist.id;
            attachmentList.add(objAttachmentt);
        }
        if(objAttachments.Body != null){
            objAttachments.ParentId = artist.id;
            attachmentList.add(objAttachments);
        }
Insert attachmentListother;
        if(attachmentList.size() > 0 && checkAttachment){
            insert attachmentList;

            // if successfully inserted new contact, then displays the thank you page.
            return Page.ack;
        }else{
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please attach at least one photo attachment.'));
            return null;
        }              
      }

 

}

My validation rule at the field level:
if(REGEX(Facebook__c,"^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$"),false,true)


Thank you.
 
Best Answer chosen by Alex Wong 4
VineetKumarVineetKumar
Only one will be shown, because the moment one validation rule fails, the code execution stops and no further validations are evaluated.
Try this on the Apex page :
Pattern emailPattern = Pattern.compile('^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$');
Boolean isMatch = emailPattern.matcher('asdasd').matches();
system.debug('#### isMatch = '+isMatch);