+ Start a Discussion
Kishore Vamsi 6Kishore Vamsi 6 

I want to disable field when I select false radio button

public class sampleCons {
    
    public string selectedValue {get;set;}
    public boolean isDisabled{set;get;}
    public sampleCons(ApexPages.StandardController controller){
        isDisabled=true;
    }
    public PageReference test() {
        if(selectedValue == 'True'){
        // perfom your logic here
        system.debug('====>'+selectedValue);
            isDisabled=true;
        } else if (selectedValue == 'False'){
            system.debug('====>'+selectedValue);
              isDisabled=false;
        // perform your logic here
        }
        return null;
    }
                
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('True','True')); 
        options.add(new SelectOption('False','False'));
        system.debug('====>'+options);
        return options; 
    }   
}
 
<apex:page standardController="Account" extensions="sampleCons">
    <apex:form id="myForm">
        <apex:pageMessages />
        <apex:selectRadio value="{!selectedValue}">
            <apex:selectOptions value="{!items}">
           
            </apex:selectOptions>
            </apex:selectRadio><p/>
        <apex:pageBlock title="Edit Contact" rendered="{!isDisabled}">
            <apex:pageBlockSection columns="1">
        <apex:inputField value="{!account.name}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
     </apex:form>
 </apex:page>

User-added image
I  am new to VF code, I have tried above code but it is not working for me. If I select true radio button then account name should be display and if I select false then it should be disabled
Best Answer chosen by Kishore Vamsi 6
Santosh Kumar 348Santosh Kumar 348
Yes that's correct and nothing is wrog with code. Account Name is mandatory field you can not keep it blank. First provide some value in the field and then check.
Else refer below code, I have moved the logic to aother field "Type". But still you need to first provide the value in Name Field as you have selected standard controller so all the validation are automatic getting fired and you can not by pass the mandatory field.
 
<apex:page standardController="Account" extensions="sampleCons">
    <apex:form id="myForm">
        <apex:pageMessages />
        
        <apex:selectRadio value="{!selectedValue}">
            <apex:selectOptions value="{!items}"/>
            <apex:actionSupport event="onchange" reRender="pbId1,pbId"/>
        </apex:selectRadio><p/>
        
        
        <apex:pageBlock title="Edit Contact" id="pbId">
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!account.name}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="1"  rendered="{!selectedValue == 'True'}">
                <apex:inputField value="{!account.Type}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class sampleCons {
    
    public string selectedValue {get;set;}
    public boolean isDisabled{set;get;}
    
   	public sampleCons(ApexPages.StandardController controller){
        selectedValue='True';
    }
    
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('True','True')); 
        options.add(new SelectOption('False','False'));
        system.debug('====>'+options);
        return options; 
    }   
}

VisibleHidden

Previous example will also work just provide some value in field.

If you find the above solution helpful. Please mark as Best Answer to help others too.

Thanks and Regards,
Santosh Kumar

All Answers

Agustin BAgustin B
Hey, check this out, it has what you need:
https://help.salesforce.com/articleView?id=000324662&type=1&mode=1
If it helps please like and mark as correct, it may help others with the same question
Santosh Kumar 348Santosh Kumar 348
Hi Kishore,

Refere the below snippet, I have done slight chnages in your code :
public class sampleCons {
    
    public string selectedValue {get;set;}
    public boolean isDisabled{set;get;}
    
   	public sampleCons(ApexPages.StandardController controller){
        selectedValue='True';
    }
    
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('True','True')); 
        options.add(new SelectOption('False','False'));
        system.debug('====>'+options);
        return options; 
    }   
}


And VF page below :
<apex:page standardController="Account" extensions="sampleCons">
    <apex:form id="myForm">
        <apex:pageMessages />
        
        <apex:selectRadio value="{!selectedValue}">
            <apex:selectOptions value="{!items}"/>
            <apex:actionSupport event="onchange" reRender="pbId" />
        </apex:selectRadio><p/>
        
        
        <apex:pageBlock title="Edit Contact" id="pbId">
            <apex:pageBlockSection columns="1"  rendered="{!selectedValue == 'True'}">
                <apex:inputField value="{!account.name}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>


So the main differece in both the code is I have used action:support and rerender attribute of action:support. 
<apex:actionSupport event="onchange" reRender="pbId" />

So if you are wondering what it does is, when you are selecting value as 'True' or 'False' you need to tell DOM that it should referesh.
Rendered is use to show and hide the component but we need to let the DOM know when to do it.
So reRender tells the dom that changes are done, so refresh the DOM.

If you find the above solution helpful. Please mark as Best Answer to help others too.

Thanks and Regards,
Santosh Kumar
 
Kishore Vamsi 6Kishore Vamsi 6
Hi Santosh, 
Thanks for the quick response.
The above code is not working, when i select false radio button I think we are not able to capture onchange value.  Any way thanks for your help.
Santosh Kumar 348Santosh Kumar 348
The code posted by me is a working model as I have checked it in my instance can you please try to paste the exact code given by me in your org and check if it is working or not.

The sample code posted by you is not your exact code and I guess you are trying to merge the logic in your code and missing out something.

So just give it a try by using my code directly create a class "sampleCons" and a VF page "sampleConsVf" and paste my code as it is if it doesn't work for you then let me know I can help you out.
Kishore Vamsi 6Kishore Vamsi 6
Hi Santosh,
I have copied same code in my instance but it is not working for me. Please see below screen shot. Even if I select false it is showing below fieldUser-added image
Santosh Kumar 348Santosh Kumar 348
Yes that's correct and nothing is wrog with code. Account Name is mandatory field you can not keep it blank. First provide some value in the field and then check.
Else refer below code, I have moved the logic to aother field "Type". But still you need to first provide the value in Name Field as you have selected standard controller so all the validation are automatic getting fired and you can not by pass the mandatory field.
 
<apex:page standardController="Account" extensions="sampleCons">
    <apex:form id="myForm">
        <apex:pageMessages />
        
        <apex:selectRadio value="{!selectedValue}">
            <apex:selectOptions value="{!items}"/>
            <apex:actionSupport event="onchange" reRender="pbId1,pbId"/>
        </apex:selectRadio><p/>
        
        
        <apex:pageBlock title="Edit Contact" id="pbId">
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!account.name}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="1"  rendered="{!selectedValue == 'True'}">
                <apex:inputField value="{!account.Type}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class sampleCons {
    
    public string selectedValue {get;set;}
    public boolean isDisabled{set;get;}
    
   	public sampleCons(ApexPages.StandardController controller){
        selectedValue='True';
    }
    
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('True','True')); 
        options.add(new SelectOption('False','False'));
        system.debug('====>'+options);
        return options; 
    }   
}

VisibleHidden

Previous example will also work just provide some value in field.

If you find the above solution helpful. Please mark as Best Answer to help others too.

Thanks and Regards,
Santosh Kumar
This was selected as the best answer
Kishore Vamsi 6Kishore Vamsi 6
Now I understood, it is working perfectly Thanks for your help. Really appreciated.