[SalesForce] How to identify the is field required from field set

I have create a field set for a custom object and i am displaying the field set on a VF page. I am not using apex:page block bec i want to change the style of the page so i am displaying using a table. I want to know whether the field is required on not if required then want to display * in the place of default required field indication by the salesforce.

<apex:page id="loginPage" showHeader="false" title="{!$Label.site.site_login}"  standardController="onboarding__c" extensions="onboarding_extCon">

    <style>
        h1{
            font-size:16px;
            padding-left:50px;
            font-family:'trebuchet ms', helvetica, sans-serif;
            color:#3366FF;
            line-height:20px;
        }
        .tableStyle{
            padding-left:50px;
            font-size:14px;
            font-family:'trebuchet ms', helvetica, sans-serif;
            color:#343434;
        }
        .cm_formCss{
             height:100%;
             margin:auto;
             width:960px;
        }
        .tdStyleFirst{
            height:25px;
            margin-bottom:13px;
            margin-left:2px;
            width:50%;
        }
        .tdStyleSecond{
            height:25px;

        }

    </style>


  <apex:composition template="{!$Site.Template}">
    <apex:define name="body">  
       <apex:form styleClass="cm_formCss" id="cmForm" >

              <h1>Onboarding</h1>
              <table class="tableStyle">              
              <apex:repeat value="{!$ObjectType.onboarding__c.FieldSets.Onboarding_Field_set}" var="f"> 

                    <tr>
                        <td class="tdStyleFirst"><apex:outputText value="{!f.Label}" />  </td>                       
                        <td><apex:inputField value="{!onboarding__c[f]}" />  </td>
                    </tr>        

              </apex:repeat>
                      <tr>
                          <td></td>
                      </tr> 
                      <tr>
                          <td></td>                          
                          <td> <apex:commandButton action="{!Save}" value="Submit"/>
                               <apex:commandButton action="{!cancel}" value="Reset"/> </td>
                      </tr>
              </table>


      </apex:form>
    </apex:define>
  </apex:composition>
</apex:page>

Please guide me if anybody have any idea.

Best Answer

You need to use getRequired() method of the FieldSetMember:

{!IF(f.Required, '*', '')}

So in your case it wiil look like this:

<tr>
    <td class="tdStyleFirst"><apex:outputText value="{!f.Label}" /></td>                       
    <td>
        <apex:outputText value="*" style="color:red;" rendered="{!f.Required}"/>
        <apex:inputField value="{!onboarding__c[f]}" />
    </td>
</tr>   
Related Topic