0

I am trying to make an input field required on the basis of selected picklist value , However, Its not working when I tried to submit the form. I have checked the value its rendering on change.

<apex:selectList multiselect="false" size="1" label="Record" styleClass="slds-select slds-size_1-of-2" value="{!FilterValue}" id="populate">
                <apex:selectOption itemValue="None" itemLabel="--None--"/>
                <apex:selectOption itemValue="Accountc" itemLabel="Account Name"/> <apex:actionSupport event="onchange" reRender="fieldBlock"/>
        </apex:selectList>
     <!-- </th>
      <th scope="col">
       <span class="slds-badge">Search Text</span> -->

       <apex:outputPanel id="fieldBlock">

          <apex:inputText label="Search Text"  value="{!Search}" styleclass="slds-input slds-size_1-of-3" html-placeholder="{!FilterValue}" id="search" required="{IF({!FilterValue}=='None',false,true)}"/>

      </apex:outputPanel>
1
  • You need to call an action, to send back your changes. If no action is called, the page just get's refreshed.
    – Basti
    Oct 27, 2017 at 6:12

1 Answer 1

1

So in your code I see multiple things to be modified.

Firstly, you are using inputText, wherein by design the required attribute simply makes the field required for the Ajax request. The red mark would not appear by default. Refer here for more details.

If possible try using inputField instead.

Also the IF condition that you have written, seems syntactically incorrect to me.

Try using it this way:

required="{!IF(FilterValue=='None',false,true)}

Also, you need to wrap the select list inside a <apex:actionRegion>, as after first selection when the inputText becomes required. The field will not be rendered onchange of picklist unless inputText has some value(as it has become required now).

Add <apex:pagemessages></apex:pagemessages> in a VisualForce Page always, so you can read the errors on the Page. In this way, you can understand whether you are getting the required input error or not.

I myself, tried this and below is a working copy of the code:

VisualforcePage

<apex:page controller="TestFieldController">
<apex:pagemessages></apex:pagemessages>
<apex:form >
<apex:pageBlock>
    <apex:pageBlockSection>
    <apex:actionRegion>
        <apex:selectList multiselect="false" size="1" label="Record" styleClass="slds-select slds-size_1-of-2" value="{!filterValue}" id="populate">
            <apex:selectOption itemValue="None" itemLabel="--None--"/>
            <apex:selectOption itemValue="Accountc" itemLabel="Account Name"/> 
            <apex:actionSupport event="onchange" reRender="fieldBlock"/>
        </apex:selectList>
    </apex:actionRegion>
    <apex:pageBlockSectionItem >
        <apex:outputLabel value="Search Text" />
        <apex:outputPanel id="fieldBlock">
        <apex:outputPanel rendered="{!AND(filterValue!='None',!ISBLANK(filterValue))}">
        <div class="requiredInput"> <div class="requiredBlock"></div>
          <apex:inputText label="Search Text"  value="{!Search}" styleclass="slds-input slds-size_1-of-3" html-placeholder="{!filterValue}" id="searchRequired" required="true"/>
        </div>
        </apex:outputPanel>
        <apex:outputPanel rendered="{!OR(filterValue=='None',ISBLANK(filterValue))}">
          <apex:inputText label="Search Text"  value="{!Search}" styleclass="slds-input slds-size_1-of-3" html-placeholder="{!filterValue}" id="searchNotRequired" required="false"/>
        </apex:outputPanel>
      </apex:outputPanel>
    </apex:pageBlockSectionItem >
    </apex:pageBlockSection>
</apex:pageBlock>
<apex:commandButton value = "Submit" action="{!callController}"/>
</apex:form>
</apex:page>

Controller:

public class TestFieldController{

  public String filterValue{get;set;}
  public String Search{get;set;}

  public void callController(){
  }
}

Here I have used two different inputText controls and rendered them accordingly as per the requirement, just to display the red bar besides the field

If you use inputField instead of inputText, you can handle this by single control.

I have added a button too, to verify whether the field gives error on submit. Let me know if you are still facing any issue.

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.