Skip to main content
Kishore Talari (Cognizant) a posé une question dans #Visualforce

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>

I want to disable field when I select false radio button

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
7 réponses
  1. 6 juin 2020, 06:22
    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;

    }

    }

    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.Hidden

    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