function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Daniel BleasdaleDaniel Bleasdale 

How to set a default value to a Radio Button?

How do I give my radio buttons a value that has already been detarmined in a picklist?
User-added image
Example if I choose "Male" in my picklist I would like the "Male" radio button to be selected on my VF page.


Also,
<div class="slds-size_2-of-8">
                                <div class="slds-box slds-box_x-small slds-text-align_center slds-m-around_x-small">
                                    <div class="slds-form-element__control">
                                        <label class="slds-form-element__label" for="form-element-02">
                                            <b>
                                                Are You British Citizen?:-
                                            </b>
                                        </label>
                                        <span class="slds-radio">
                                            <input type="radio" id="radio-3" value="radio-" name="YesNo" />
                                            <label class="slds-radio__label" for="radio-3">
                                                <span class="slds-radio_faux">
                                                </span>
                                                <span class="slds-form-element__label">
                                                    Yes
                                                </span>
                                            </label>
                                        </span>
                                        <span class="slds-radio">
                                            <input type="radio" id="radio-4" value="radio-4" name="YesNo" />
                                            <label class="slds-radio__label" for="radio-4">
                                                <span class="slds-radio_faux">
                                                </span>
                                                <span class="slds-form-element__label">
                                                    No
                                                </span>
                                            </label>
                                        </span>
                                    </div>
                                    {! Apprentice__c.British_Citizen__c}
                                </div>
                            </div>

how do I use a checkbox with a value of TRUE & FALSE and convert it into a radio button saying yes and no?
User-added image
 
<div class="slds-size_2-of-8">
                                <div class="slds-box slds-box_x-small slds-text-align_center slds-m-around_x-small">
                                    <div class="slds-form-element__control">
                                        <label class="slds-form-element__label" for="form-element-01">
                                            <b>
                                                Gender
                                            </b>
                                        </label>
                                        <span class="slds-radio">
                                            <input type="radio" id="radio-1" value="{! Apprentice__c.Gender__c}" name="MaleFemale"/>
                                            <label class="slds-radio__label" for="radio-1">
                                                <span class="slds-radio_faux">
                                                </span>
                                                <span class="slds-form-element__label">
                                                    Male
                                                </span>
                                            </label>
                                        </span>
                                        <span class="slds-radio">
                                            <input type="radio" id="radio-2" value="{! Apprentice__c.Gender__c}" name="MaleFemale" />
                                            <label class="slds-radio__label" for="radio-2">
                                                <span class="slds-radio_faux">
                                                </span>
                                                <span class="slds-form-element__label">
                                                    Female
                                                </span>
                                            </label>
                                        </span>
                                    </div>
                                </div>
                            </div>

Thanks Dan
Best Answer chosen by Daniel Bleasdale
Raj VakatiRaj Vakati
Step 1 : Creat an apex class that will get the list values .. Sample code
public List<SelectOption> getTypes(){
            Schema.sObjectType sobject_type = customObject__c.getSObjectType();


            Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe();


            Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap();
          
            List<Schema.PicklistEntry> pick_list_values = field_map.get('type__c').getDescribe().getPickListValues();


            List<selectOption> options = new List<selectOption>();


           for (Schema.PicklistEntry a : pick_list_values) {
                      options.add(new selectOption(a.getLabel(), a.getValue()));
          }
      return options;


Step 2 : Get the Currect Selected valule from the Object .. You can use SOQL to get the value and set to the attribute 

Step 3 : Do the aura:iteration  to loop the values from the picklist 

Step 4 : Use select option select to true if the picklist value is matched the data form the record 



 

All Answers

Raj VakatiRaj Vakati
Step 1 : Creat an apex class that will get the list values .. Sample code
public List<SelectOption> getTypes(){
            Schema.sObjectType sobject_type = customObject__c.getSObjectType();


            Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe();


            Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap();
          
            List<Schema.PicklistEntry> pick_list_values = field_map.get('type__c').getDescribe().getPickListValues();


            List<selectOption> options = new List<selectOption>();


           for (Schema.PicklistEntry a : pick_list_values) {
                      options.add(new selectOption(a.getLabel(), a.getValue()));
          }
      return options;


Step 2 : Get the Currect Selected valule from the Object .. You can use SOQL to get the value and set to the attribute 

Step 3 : Do the aura:iteration  to loop the values from the picklist 

Step 4 : Use select option select to true if the picklist value is matched the data form the record 



 
This was selected as the best answer
Daniel BleasdaleDaniel Bleasdale
Does that mean im going to have to test the code coverage on that Class too.?
Ajay K DubediAjay K Dubedi
Hi Daniel,

Below code can fullfill your requirements. Hope this will work for you.

VF page :

<apex:page controller="sampleCon">
    <apex:form>
        <apex:selectRadio value="{!Gender}">
            <apex:selectOptions value="{!items}"/>
        </apex:selectRadio><p/>
        <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
    </apex:form>
    <apex:outputPanel id="out">
        <apex:actionstatus id="status" startText="testing..."> 
            <apex:facet name="stop"> 
                <apex:outputPanel> 
                    <p>You have selected:</p> 
                    <apex:outputText value="{!Gender}"/> 
                </apex:outputPanel> 
            </apex:facet> 
        </apex:actionstatus> 
    </apex:outputPanel> 
</apex:page>

Controller :

public class sampleCon {
    String Gender = null;
    public PageReference test() {
        return null;
    }
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('Male','Male')); 
        options.add(new SelectOption('Female','Female')); 
        return options; 
    } 
    public String getGender() {
        return Gender;
    } 
    public void setGender(String Gender) { this.Gender = Gender; }
}

    
Please mark as best answer if it helps you.

Thank You 
Ajay Dubedi