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
babloo123babloo123 

VF page required fields

I have multiselect Checkboxes for a questions like certifications
Salesforce Admin
Salesforce Advance Admin
Salesforce Developer
Other

So I can select as many as I want but if I select other then there is a text field below to enter what the other certifications are but how to make the below question mandatory if in the previous question only if other is selected?
Best Answer chosen by babloo123
lakslaks
Hi Babloo,

The below piece of code will give you an idea about what I was trying to explain.
You can modify it to meet your requirement.

Visualforce page:
<apex:page controller="PageController">
    <apex:form >
      <apex:selectCheckboxes value="{!certVals}">
          <apex:selectOptions value="{!items}"></apex:selectOptions>
          <apex:actionSupport reRender="othercert" event="onchange" action="{!othercheck}"/>          
      </apex:selectCheckboxes> 
      
     <apex:outputPanel id="othercert">
         <apex:inputText rendered="{!showField}" value="{!otherCertVal}"></apex:inputText>          
     </apex:outputPanel>
      
    </apex:form> 
</apex:page>

Controller:
public with sharing class PageController 
{

    public String otherCertVal { get; set; }
    
    String[] certVals = new String[]{};
    
    public Boolean showField{ get; set; }
    
    public PageController()
    {
        showField = False;
    }
    
    public List<SelectOption> getItems() 
    {        
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Salesforce Admin','Salesforce Admin'));
        options.add(new SelectOption('Salesforce Advance Admin','Salesforce Advance Admin'));
        options.add(new SelectOption('Salesforce Developer','Salesforce Developer'));
        options.add(new SelectOption('Other','Other'));
        return options;
    }
    
    public String[] getcertVals() 
    {
        return certVals; 
    }
    
    public void setcertVals(String[] certVals)
    {
        this.certVals = certVals;
    }
    
    public pageReference othercheck()
    {
        if(!certVals.isEmpty() && certVals[0].equalsIgnoreCase('Other'))
        {
            showField = TRUE;
        }
        else if(certVals.isEmpty())
        {
            showField = False;  
        }
        else
        {
            showField = False;  
        }
        return null;
    }    
}

Hope this helps.

Please do not forget to mark it as Solution if it solved your problem.

Regards,
Lakshmi.
 

All Answers

lakslaks
You can keep the textfield to enter additional certifications invisible/deactivated. And only when the 'Other' checkbox is checked it should be visible/editable.
babloo123babloo123
Can you guide me how to do it I am stuck on this issue
lakslaks
To make the field visible based on condition you can use the rendered attribute in the text field with a Boolean variable as its value which you can set to True/False as per your conditions.

<apex:inputText...... rendered="{!showField}" />

Wherever your conditional logic is written, you can set the Boolean variable showField to True/False.

Hope this helps.

Regards,
Lakshmi.
lakslaks
And you can make the field mandatory using the 'required' attribute.
babloo123babloo123
Hi Laks,

           Thanks a lot for the info but I am not getting how to write code for the same can you please help? May be a random example can you please help

 
lakslaks
Hi Babloo,

The below piece of code will give you an idea about what I was trying to explain.
You can modify it to meet your requirement.

Visualforce page:
<apex:page controller="PageController">
    <apex:form >
      <apex:selectCheckboxes value="{!certVals}">
          <apex:selectOptions value="{!items}"></apex:selectOptions>
          <apex:actionSupport reRender="othercert" event="onchange" action="{!othercheck}"/>          
      </apex:selectCheckboxes> 
      
     <apex:outputPanel id="othercert">
         <apex:inputText rendered="{!showField}" value="{!otherCertVal}"></apex:inputText>          
     </apex:outputPanel>
      
    </apex:form> 
</apex:page>

Controller:
public with sharing class PageController 
{

    public String otherCertVal { get; set; }
    
    String[] certVals = new String[]{};
    
    public Boolean showField{ get; set; }
    
    public PageController()
    {
        showField = False;
    }
    
    public List<SelectOption> getItems() 
    {        
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Salesforce Admin','Salesforce Admin'));
        options.add(new SelectOption('Salesforce Advance Admin','Salesforce Advance Admin'));
        options.add(new SelectOption('Salesforce Developer','Salesforce Developer'));
        options.add(new SelectOption('Other','Other'));
        return options;
    }
    
    public String[] getcertVals() 
    {
        return certVals; 
    }
    
    public void setcertVals(String[] certVals)
    {
        this.certVals = certVals;
    }
    
    public pageReference othercheck()
    {
        if(!certVals.isEmpty() && certVals[0].equalsIgnoreCase('Other'))
        {
            showField = TRUE;
        }
        else if(certVals.isEmpty())
        {
            showField = False;  
        }
        else
        {
            showField = False;  
        }
        return null;
    }    
}

Hope this helps.

Please do not forget to mark it as Solution if it solved your problem.

Regards,
Lakshmi.
 
This was selected as the best answer
babloo123babloo123
Thx a lot Lakshmi the code did work but says 


apex:inputFile can not be used in conjunction with an action component, apex:commandButton or apex:commandLink that specifies a rerender or oncomplete attribute. 

I think this is because I have attachment in my VF page if you know can you help me overcome this issue or anyother way to make a validation just saying required as other is selected?