0

I am displaying fields from fieldset on VF page using repeat, but I am unable to validate the emptiness of required field. I want to display error message in case of empty required field.

VF Page:

<apex:page controller="IncidentDetails">
    <apex:form >   
        <apex:commandButton value="{!$Label.SSSubmit}" title="{!$Label.SS_ProfileSubmit} (Ctrl+Alt+S)"  action="{!saveUserProfile}" />
        <apex:repeat value="{!fields}" var="f">                   
            <table>
                <tr>                 
                    <td valign="top" align="left" width="120px">

                    <apex:outputText value="{!f.Label}"  /> 

                    <apex:outputText value="*" style="color:red;" rendered="{!OR(f.DBRequired, f.required)}" />
                    </td> 

                    <td valign="top" align="left" title="{!$ObjectType.User.fields[f].inlineHelpText}">
                    <apex:inputField value="{!userobj[f.fieldPath]}" 
                    required="{!OR(f.required, f.dbrequired)}"/>
                    </td>
                </tr>
            </table>
        </apex:repeat>
    </apex:form>  
</apex:page>

Controller:

public class IncidentDetails{

    public User userobj { get; set; }
    public string userid { get; set; }
    public Integer i{get; set;}

    public IncidentDetails() {
        this.userobj = getUserDetails();
    }

    public List<Schema.FieldSetMember> getFields() {
        return SObjectType.User.FieldSets.Self_Service_My_Profile.getFields();
    }

    private User getUserDetails() { 
        userid = UserInfo.getUserId();

        String query = 'SELECT ';
        for(Schema.FieldSetMember f : this.getFields()) {
            query += f.getFieldPath() + ', ';
        }
        query += 'Id FROM User where Id=:userid LIMIT 1';
        return Database.query(query);
    }

    public PageReference saveUserProfile(){ 
        system.debug('saveuser');       

        try{
            update userobj;
            system.debug('in update');
        }catch(DMLException e){
            system.debug('in exception');
        }               

        return null;
    }
}

Commandbutton does not follow action function in case of I try to save the record with empty required field. Even 'Update' operation doesn't give exception in above case.

ugo
  • 2,705
  • 2
  • 30
  • 34

1 Answers1

0

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm and the second link in https://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_lifecycle.htm

Fields that are marked as required by page layout (be it standard page layout or field required on VF page) are checked before the actual action method is called. So the setter of your values will complain about missing data even before the control is passed to your function and your code will have a chance to throw a custom error message.

You can also verify this behaviour with debug logging enabled or by having Developer Console open. I'm pretty sure there won't be system.debug('saveuser'); in that log.

In other words - you don't have to do anything, the standard error about missing value should be OK? After all - it would be consistent with how the rest of Salesforce works, you shouldn't surprise your users with some custom error handling...

If you really want to write such custom validation - you mustn't mark the fields as required on the page so the setter methods will be able to finish and actually pass the data from HTML to your controller's variables.

eyescream
  • 18,088
  • 2
  • 34
  • 46