5

I have a simple radio button that specifies the type of position:

<div class="lead">Position</div>
     <apex:pageBlockSection columns="1"> 
        <apex:selectRadio value="{!Job__c.Type_of_Position__c}" layout="lineDirection">
        <apex:selectOption itemValue="Mentor" itemlabel="Mentor"/>
        <apex:selectOption itemValue="Consultant" itemlabel="Consultant"/>
        <apex:selectOption itemValue="Permanent" itemlabel="Permanent"/>
        </apex:selectRadio>
    </apex:pageBlockSection>

Any idea how I can set the default value to Mentor?

I tried setting a variable in the controller:

// Setting Default Options  
   public String posType = 'Mentor';

    public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('Mentor','Mentor'));
    options.add(new SelectOption('Consultant','Consultant'));
    options.add(new SelectOption('Permanent','Permanent')); return options;
    }

    public String getPosType() {
    return posType;
        }

    public void setPosType (String posType) { 

        this.posType = posType; } 

And the VF

 <div class="lead">Position</div>
     <apex:pageBlockSection columns="1"> 
        <apex:selectRadio value="{!posType}" layout="lineDirection">
        <apex:selectOptions value="{!items}"/>
        </apex:selectRadio>
    </apex:pageBlockSection>

But no value is added when the form is submitted. How can I return the value selected?

2
  • Change public String posType = 'Mentor'; to String posType = 'Mentor';.Guess this should work – Eagerin Sf May 27 '13 at 14:47
  • It didn't work, still no value. – gato negro May 27 '13 at 15:23
5

You need to define the posType with getter/setter and set the default value in your constructor, the rest is correct:

public String posType { get; set; }

public YourClass(){
    posType = 'Consultant';
}

public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('Mentor','Mentor'));
    options.add(new SelectOption('Consultant','Consultant'));
    options.add(new SelectOption('Permanent','Permanent')); 
    return options;
}

Page:

<apex:selectRadio value="{!posType}" layout="lineDirection">
    <apex:selectOptions value="{!items}"/>
</apex:selectRadio>

The code above works on my dev org.

3
  • It passes the value, but doesn't display the default value to the client on the frontend. – gato negro May 27 '13 at 15:23
  • @gatonegro The code above works for me. Check the selectRadio structure. – Sergej Utko May 27 '13 at 17:48
  • It defaults on the front end to have the consultant selected? – gato negro May 28 '13 at 20:38

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.