1

I have a VF Page with a lot of fields and most of them are mandatory. I am handling the required part via my Apex Controller and not "required=true" attribute as I have used too much Javascript on my Page. Now the Issue is that Users want to see the PageMessage errors below the Field value as well as on top of the Page - But if a have the code like fieldname.adderror('You must enter a value') for all the 100+ fields then it shows only once on top and rest all is shown below the field values only. Is there a way to not suppress the error messages on top of the page.

This is the controller code snippet:

        if(Obj.field1 == null){
        field1.addError('You must enter a value!');
    }
    if(Obj.field2 == null){
        field2.addError('You must enter a value!');
    }

The above code throws the error only for field1 on top of the page and for field2 its shown below the field only and not on top of the page.

Note I have a workaround of adding blank spaces on the error message like for eg.

        field2.addError('You must enter a value!   ');

which treats the messages as separate entities and works properly as expected - I was just looking for a more decent approach

1

This code might help you.

Vf Page Code :

<apex:page controller="CtrlValidationErrorAtTwoPlaces">
<H1>Display error msg at two places.</h1>

<br/><br/>

<apex:pageMessages ></apex:pageMessages>
<apex:form >

   <apex:outputText value="Enter Name : "/>
   <apex:inputField value="{!Acc.Name}"/><br/><br/><br/>

   <apex:outputText value="Enter description: "/>
   <apex:inputField value="{!Acc.description}"/><br/><br/><br/>

  <apex:commandButton value="Submit" action="{!Submit}"/>
</apex:form>

and Controller code :

public Class CtrlValidationErrorAtTwoPlaces{

public Account Acc{get;set;}

public CtrlValidationErrorAtTwoPlaces()
{
    Acc = new Account();
}

public PageReference Submit() {
    if(Acc.Name != null && Acc.Name.length() > 2)
          Acc.Name.addError('Please enter description with 5 char!');

    if(Acc.description != null && Acc.description.length() > 5)
          Acc.description.addError('Please enter description with 5 char');

    return null;
}
}

And the result will come like this. enter image description here

1
  • This is what i ended up doing. Thanks for the suggestion!
    – Sid
    Sep 19 '17 at 7:22
0

you can add ApexPages and then display them in UI. They always display as seperate error messages.

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Message'));
3
  • Yes. Thats correct - but the requirement is to show the error message inline to the field and on top of the page both!
    – Sid
    Aug 23 '17 at 13:28
  • @Sid you need both field add error and page messages Aug 23 '17 at 16:00
  • Thanks for the suggestion - I tried that, but it gives me the same error twice on top of the page which again is confusing - guess its showing both the field level error message and the apexpages.adderror on top of the page and also inline to the field. So is there any way to avoid that?
    – Sid
    Aug 30 '17 at 7:25
0

If you are using a standard controller, Create two validation rules with the same error message on the same field, one error location at the field, and another with the error location at top of the page.

Use <apex:pageMessages > in visualforce page so that the error message will get display at two places.

2
  • Its a custom Controller - The field is mandatory only at the form level and not on the record level itself. Also don't want to create 100+ validation rules just for this purpose.
    – Sid
    Aug 23 '17 at 13:48
  • For a custom controller, you would need field.addError to display it on the field and ApexPages.addMessage() to display on the top.You might want to check to see if at least 1 of the fields have errored out to display the top message.
    – SF Learner
    Aug 23 '17 at 22:42

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.