1

I have a few validation rules which display errors on fields. This is working fine for Standard Pages. But when these validations fail on a VisualForce page, the error messages show on the top of the page in "apex:messages". Is there a way to show these messages on the fields, without re-creating the validation checks in Apex or VisualForce page.

UPDATE: For example:

APEX:

public PageReference save() {
    try
    {
        //controller.save();
        update application;
    }
    catch(DmlException e)
    {
        application.addError(e);
        //ApexPages.addMessages(e);
        return null;
    }
    catch (Exception e)
    {
        //application.addError(e);
        ApexPages.addMessages(e);
        return null;
    }

    return null;
}

VISUALFORCE

<apex:outputpanel id="Messages">
    <strong><apex:messages style="color:red;"/></strong>
</apex:outputpanel>
<apex:inputField value="{!application.Field_Having_Validation_Rule__c}" />
4
  • 1
    Could you show us a bit the current code of your VF page ?
    – brovasi
    Commented Jun 2, 2015 at 6:30
  • I have updated the original post with the code.
    – MAli
    Commented Jun 2, 2015 at 6:37
  • I have solved half of my problem. I missed the part which was causing the error in my original post. This line was causing the error: <apex:commandButton action="{!save}" value="Save" reRender="Messages" status="saveStatus" />. When I removed the reRender attribute, its showing the error on the field. The problem now is that its showing the error on the field as well as top of the page.
    – MAli
    Commented Jun 3, 2015 at 4:34
  • Possibly relevant Ideas Exchange post.
    – Adrian Larson
    Commented Jun 4, 2015 at 23:40

3 Answers 3

0

You need to reference the id of the inputField element. You will have to create one apex:message for each field I think, not too sure can't remember.

<apex:inputField value="{!application.Field_Having_Validation_Rule__c}" id="Field_Having_Validation_Rule"/> 
...
<apex:message for="Field_Having_Validation_Rule" styleClass="errorMessage" />
0

You can find your solution here :

Field level error message

Check this one also VF field level error message

1
  • Both of these posts require checking for the validation errors in the controller. I do not want to check for these validation errors again as they are defined in validation rules. I need this to work: "salesforce.com/docs/developer/pages/Content/…"
    – MAli
    Commented Jun 2, 2015 at 23:29
0

After a bit of trial and error of the Salesforce example:

Salesforce Field Validation Example

I figured out the following:

  1. Anything in reRender attribute will cause the validation errors to NOT show.
  2. The duplication of error message is a known issue with Salesforce apex:messages (Known Issue)

For the time being I will consider this as the best solution without any extra coding.

Please let me know if there is any other way of doing it.

Cheers

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .