1

I have few custom validations, which i use in my controller. And i found that not for all fields it works correctly. I figured out that it shows only message, but doesnt show error near the inputField area, if the field type is not a text or a number. And everything is ok, if i remove inlineEditSupport

My part of vf page:

<apex:pageMessages id="messages" />    
<apex:pageBlockTable  value="{!questions}" var="question" >
                  <apex:column headerValue="field 1" >
                       <apex:outputField value="{!question.field1__c}" >
                           <apex:inlineEditSupport event="ondblClick" />
                       </apex:outputField>
                    </apex:column>
                    <apex:column headerValue="field 2" >
                       <apex:outputField value="{!question.field2__c}" >
                           <apex:inlineEditSupport event="ondblClick" />
                       </apex:outputField>
                    </apex:column>             
                    <apex:column headerValue="field 3" >
                       <apex:outputField value="{!question.field3__c}" >
                           <apex:inlineEditSupport event="ondblClick" />
                       </apex:outputField>
                    </apex:column>

In controller:

for (Question__c question : questions) {
   question.field1__c.addError('field1 is required');
   question.field2__c.addError('field2 is required');
   question.field3__c.addError('field3 is required');
}

Any ideas, why that happens and how to fix it?

1

This has to do with how inlineEdit mechanism is Salesforce is realised: outputField has a paired inputField element, and only one is rendered at a time, depending in what mode you are. addError in its order can only render error message for input field, but it's not rendered at the time when validation occur.

Here is what I'm using to validate required fields on VF page with inlineEditSupport:

Disclaimer: code below could include other people's code, more likely a combination of them, but I don't remember from where it was taken, and it also has a good portion of my trial and error's

VF page

<apex:pageBlockSection >
<apex:inlineEditSupport rendered="{!shouldDisable == 0}" />

    <apex:repeat value="{!$ObjectType.Action__c.FieldSets.EditAction}" var="fieldValue">
        <!--  only one field from the pair is be rendered at the time -
            required has reqField id used by jQuery to mark it -->
        <apex:outputField id="reqField" value="{!aLine[fieldValue]}"
            rendered="{!fieldValue.Required || fieldValue.DBRequired}"/>
        <apex:outputField value="{!aLine[fieldValue]}"
            rendered="{! !(fieldValue.Required || fieldValue.DBRequired)}"/>
    </apex:repeat>
</apex:pageBlockSection>

<!-- ===== javascript ===== -->

var j$ = jQuery.noConflict();
var reqMarked = false;

j$(document).ready(function() {
    j$('.inlineEditWrite,.inlineEditWriteOn')
        .dblclick(function() {
            if (!reqMarked) {
                markRequired();
            }
    });
});

function markRequired() {
    j$('[id*=reqField].inlineEditDiv, .inlineEditGroup')
        .addClass('inlineEditRequiredDiv')
        .removeClass('inlineEditDiv');

    j$('[id*=reqField_ileinneredit].inlineEditCompoundDiv')
        .addClass('inlineEditRequiredDiv');
}

In controller:

public pageReference onSave()
{
    pageReference pr = null; // refresh the page by default
    fieldsValidation();
    // commit changes if there is no validation (or any other) page error messages
    if (ApexPages.getMessages().size() == 0)
    {
        try
        {
            update aLine;
            pr = new pageReference('/' + aLine.Id);
        }
        catch(DmlException ex)
        {
            ApexPages.addMessages(ex);
            for (Integer i = 0; i < ex.getNumDml(); i++)
            {
                // Process exception here
                System.debug(ex.getDmlMessage(i)); 
             }
            System.debug('DmlException caught: ' + ex.getMessage());
        }
    }
return pr;
}

public void fieldsValidation()
{
// validation on required fields from 2 fieldsets   
    List<Schema.FieldSetMember> editAction = SObjectType.Action__c.FieldSets.EditAction.getFields();
    editAction.addAll(SObjectType.Action__c.FieldSets.EditAction_Wide.getFields());

    String listRequired = ''; // Will hold all blank required filed labels

    for(integer a=0;a<editAction.size();a++)
    {
        if((editAction[a].DBRequired || editAction[a].Required) &&
         (aLine.get(editAction[a].getFieldPath()) == null || aLine.get(editAction[a].getFieldPath()) == ''))
        {
            listRequired += ', ' + editAction[a].Label;
        }
    }
    if (!String.isBlank(listRequired))
    {
        aLine.addError('The following field' + (listRequired.indexOf(',', 1) == -1 ? ' is ' : 's are ') +
            '<u>required</u>: <b>' + listRequired.removeStart(', ') + '</b>');
    }
}
  • Hi Oleksiy, thanks for the answer. What is aLine in your code? – Bogdan K Mar 24 '16 at 9:39
  • Sorry, I didn't modify my code, but as you may figured out, my code is for Action__c custom object, so aLine is a record retrieved by standard controller: aLine = (Action__c) sController.getRecord(); – o-lexi Mar 25 '16 at 16:51

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.