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
sfdotcomsfdotcom 

Visualforce page reRender

Hi,
I have created an object. Object Name: Text
Fieds:
1. Text Name(Standard field)
2. values (picklist field: values: 1, 2, 3)
3. Text 1 (Text field)
4. Text 2 (Text field)
5. Text 3 (Text field)

Now, I need a vf page. If I select picklist value as '1', then 'Text 1 ' field should be marked as required. If I select value '2', then 'Text 2 ' should be marked as required(red). Here we have to use reRender attribute and fields should be marked as required before saving.


<apex:page standardController="Text__c">
<apex:form>
<apex:pageBlock >
<apex:pageBlockSection id="thePageBlockSection">
<apex:inputField value="{!Text__c.Name}"/>
<apex:inputField value="{!Text__c.Text_values__c}" /> 
<apex:inputField  value="{!Text__c.Text_1__c}" required="{!if(Text__c.Text_values__c== '1'), true, false}"/>
<apex:inputField  value="{!Text__c.Text_2__c}" required="{!if(Text__c.Text_values__c== '2'), true, false}"/> 
<apex:inputField  value="{!Text__c.Text_3__c}" required="{!if(Text__c.Text_values__c== '3'), true, false}"/> 

</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Thank You.
Best Answer chosen by sfdotcom
hitesh90hitesh90
Hello,

Try below code for your requirement.

Visualforce Page:
<apex:page standardController="Text__c">
    <apex:form >
        <apex:pageBlock>            
            <apex:pageBlockSection id="thePageBlockSection1">
                <apex:pageblockSectionItem>
                    <apex:outputLabel value="Text Name"></apex:outputLabel>
                    <apex:inputField value="{!Text__c.Name}"/>
                </apex:pageblockSectionItem>
                <apex:pageblockSectionItem>                    
                    <apex:outputLabel value="Text values"></apex:outputLabel>
                    <apex:actionRegion>
                    <apex:inputField value="{!Text__c.Text_values__c}">
                        <apex:actionSupport event="onchange" reRender="opTxt1, opTxt2, opTxt3"/>
                    </apex:inputField>
                    </apex:actionRegion>
                </apex:pageblockSectionItem>                
                <apex:pageblockSectionItem id="pbsItem1">                    
                    <apex:outputLabel value="Text 1"></apex:outputLabel>
                    <apex:outputPanel id="opTxt1">
                        <apex:inputField id="txt1" value="{!Text__c.Text_1__c}" required="{!if(Text__c.Text_values__c== '1', true, false)}"/>
                    </apex:outputPanel>
                </apex:pageblockSectionItem>                
                <apex:pageblockSectionItem id="pbsItem2">                    
                    <apex:outputLabel value="Text 2"></apex:outputLabel>
                    <apex:outputPanel id="opTxt2">
                        <apex:inputField id="txt2" value="{!Text__c.Text_2__c}" required="{!if(Text__c.Text_values__c== '2', true, false)}"/>
                    </apex:outputPanel>
                </apex:pageblockSectionItem>                
                <apex:pageblockSectionItem id="pbsItem3">                    
                    <apex:outputLabel value="Text 3"></apex:outputLabel>
                    <apex:outputPanel id="opTxt3">
                        <apex:inputField id="txt3" value="{!Text__c.Text_3__c}" required="{!if(Text__c.Text_values__c== '3', true, false)}"/>
                    </apex:outputPanel>
                </apex:pageblockSectionItem>                             
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
 

All Answers

CongnizentCongnizent
Hi please move code as 

<apex:page standardController="Text__c">
<apex:form>
<apex:pageBlock >
<apex:pageBlockSection id="thePageBlockSection">
<apex:inputField value="{!Text__c.Name}"/>
<apex:actionRegion >
<apex:inputField value="{!Text__c.Text_values__c}" /> 
<apex:actionSupport event="onchange" reRender="1,2,3" />
</apex:inputField>
</apex:actionRegion>

<apex:inputField  id ="1" value="{!Text__c.Text_1__c}" required="{!if(Text__c.Text_values__c== '1'), true, false}"/>
<apex:inputField  id ="2" value="{!Text__c.Text_2__c}" required="{!if(Text__c.Text_values__c== '2'), true, false}"/> 
<apex:inputField   id ="3" value="{!Text__c.Text_3__c}" required="{!if(Text__c.Text_values__c== '3'), true, false}"/> 

</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
CongnizentCongnizent
Please let me know if it works or not 

Thanks u so much .
sfdotcomsfdotcom
Hi Cognizent, 
I am getting error as: "Error:1"
CongnizentCongnizent
ok friend , let me try 5 min plz
CongnizentCongnizent
Yes bro , I tried it and replace code as


<apex:page standardController="Text__c">
<apex:form>
<apex:pageBlock >
<apex:pageBlockSection id="thePageBlockSection">
<apex:inputField value="{!Text__c.Name}"/>
<apex:actionRegion >
<apex:inputField value="{!Text__c.Text_values__c}" /> 
<apex:actionSupport event="onchange" reRender="as,bs,cs,ds" />
</apex:inputField>
</apex:actionRegion>
 <apex:outputPanel id="ds">
<apex:inputField  id ="as" value="{!Text__c.Text_1__c}" required="{!if(Text__c.Text_values__c== '1'), true, false}"/>
<apex:inputField  id ="bs" value="{!Text__c.Text_2__c}" required="{!if(Text__c.Text_values__c== '2'), true, false}"/> 
<apex:inputField   id ="cs" value="{!Text__c.Text_3__c}" required="{!if(Text__c.Text_values__c== '3'), true, false}"/> 
</apex:outputPanel >
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>


actually i tried below code and it's working fine as expected so check it if above is not working now .


<apex:page standardController="Contact">
<apex:form >
 <apex:pageblock >
    <apex:pageblockSection >
    <apex:outputPanel layout="block" >
         <apex:outputField value="{!Contact.Name}"/>
         <apex:actionRegion >
            <apex:inputField value="{!Contact.Padm__Demo_Picklist__c}" > 
            <apex:actionSupport event="onchange" reRender="as,bs,cs,ds" />
            </apex:inputField>
        </apex:actionRegion>
        <apex:outputPanel id="ds">
        <apex:inputField  id ="as" value="{!Contact.Padm__Demo_text__c}" required="{!(Contact.Padm__Demo_Picklist__c== 'English')}"/>
        <apex:inputField  id ="bs" value="{!Contact.Padm__Demo_text__c}" required="{!(Contact.Padm__Demo_Picklist__c== 'hindi')}"/> 
        <apex:inputField   id ="cs" value="{!Contact.Padm__Demo_text__c}" required="{!(Contact.Padm__Demo_Picklist__c== '3')}"/> 
       </apex:outputPanel >
     </apex:outputPanel>
     
    </apex:pageblockSection>
 </apex:pageblock>
 </apex:form>
</apex:page>
Arunkumar RArunkumar R
Hi Sfdotcom,

Try the below one, If alignment issue change it accordingly using pageblocksection or css style,
 
<apex:page standardController="Text__c">
	<apex:form>
		<apex:pageBlock >
			<apex:pageBlockSection id="thePageBlockSection">
				<apex:inputField value="{!Text__c.Name}"/>
				<apex:actionRegion>
					<apex:pageBlockSection>
						<apex:inputField value="{!Text__c.Text_values__c}" >
						<apex:actionSupport event="onchange" reRender="requiredPanel"/>
						<apex:inputField>
					</apex:pageBlockSection>
				</apex:actionRegion>
				<apex:outputPanel id="requiredPanel">
				<apex:pageBlockSection>
				<apex:inputField  value="{!Text__c.Text_1__c}" required="{!if(Text__c.Text_values__c== '1'), true, false}"/>
				<apex:inputField  value="{!Text__c.Text_2__c}" required="{!if(Text__c.Text_values__c== '2'), true, false}"/> 
				<apex:inputField  value="{!Text__c.Text_3__c}" required="{!if(Text__c.Text_values__c== '3'), true, false}"/> 
				</apex:pageBlockSection>
				</apex:outputPanel>
			</apex:pageBlockSection>
			
		<apex:pageBlockButtons >
			<apex:commandButton value="Save" action="{!save}"/>
		</apex:pageBlockButtons>
		
		</apex:pageBlock>
	</apex:form>
</apex:page>

 
sfdotcomsfdotcom
Hi Arunkumar,
I got Syntax error. Extra ','
sfdotcomsfdotcom
Hi Cognizent,
I am getting below error:
Error: text2 line 9, column 3: The element type "apex:actionregion" must be terminated by the matching end-tag "</apex:actionregion>" [Error] Error: The element type "apex:actionregion" must be terminated by the matching end-tag "</apex:actionregion>".
Arunkumar RArunkumar R
Can you try the below one. If you still getting error, then please let me know the line number,
 
<apex:page standardController="Text__c">
	<apex:form>
		<apex:pageBlock >
			<apex:pageBlockSection id="thePageBlockSection">
				<apex:inputField value="{!Text__c.Name}"/>
				<apex:actionRegion>
					<apex:pageBlockSection>
						<apex:inputField value="{!Text__c.Text_values__c}" >
						<apex:actionSupport event="onchange" reRender="requiredPanel"/>
						<apex:inputField>
					</apex:pageBlockSection>
				</apex:actionRegion>
				<apex:outputPanel id="requiredPanel">
				<apex:pageBlockSection>
				<apex:inputField  value="{!Text__c.Text_1__c}" required="{!if(Text__c.Text_values__c== '1', true, false)}"/>
				<apex:inputField  value="{!Text__c.Text_2__c}" required="{!if(Text__c.Text_values__c== '2', true, false)}"/> 
				<apex:inputField  value="{!Text__c.Text_3__c}" required="{!if(Text__c.Text_values__c== '3', true, false)}"/> 
				</apex:pageBlockSection>
				</apex:outputPanel>
			</apex:pageBlockSection>
			
		<apex:pageBlockButtons >
			<apex:commandButton value="Save" action="{!save}"/>
		</apex:pageBlockButtons>
		
		</apex:pageBlock>
	</apex:form>
</apex:page>

 
hitesh90hitesh90
Hello,

Try below code for your requirement.

Visualforce Page:
<apex:page standardController="Text__c">
    <apex:form >
        <apex:pageBlock>            
            <apex:pageBlockSection id="thePageBlockSection1">
                <apex:pageblockSectionItem>
                    <apex:outputLabel value="Text Name"></apex:outputLabel>
                    <apex:inputField value="{!Text__c.Name}"/>
                </apex:pageblockSectionItem>
                <apex:pageblockSectionItem>                    
                    <apex:outputLabel value="Text values"></apex:outputLabel>
                    <apex:actionRegion>
                    <apex:inputField value="{!Text__c.Text_values__c}">
                        <apex:actionSupport event="onchange" reRender="opTxt1, opTxt2, opTxt3"/>
                    </apex:inputField>
                    </apex:actionRegion>
                </apex:pageblockSectionItem>                
                <apex:pageblockSectionItem id="pbsItem1">                    
                    <apex:outputLabel value="Text 1"></apex:outputLabel>
                    <apex:outputPanel id="opTxt1">
                        <apex:inputField id="txt1" value="{!Text__c.Text_1__c}" required="{!if(Text__c.Text_values__c== '1', true, false)}"/>
                    </apex:outputPanel>
                </apex:pageblockSectionItem>                
                <apex:pageblockSectionItem id="pbsItem2">                    
                    <apex:outputLabel value="Text 2"></apex:outputLabel>
                    <apex:outputPanel id="opTxt2">
                        <apex:inputField id="txt2" value="{!Text__c.Text_2__c}" required="{!if(Text__c.Text_values__c== '2', true, false)}"/>
                    </apex:outputPanel>
                </apex:pageblockSectionItem>                
                <apex:pageblockSectionItem id="pbsItem3">                    
                    <apex:outputLabel value="Text 3"></apex:outputLabel>
                    <apex:outputPanel id="opTxt3">
                        <apex:inputField id="txt3" value="{!Text__c.Text_3__c}" required="{!if(Text__c.Text_values__c== '3', true, false)}"/>
                    </apex:outputPanel>
                </apex:pageblockSectionItem>                             
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
 
This was selected as the best answer
CongnizentCongnizent
finally try it 


<apex:page standardController="Text__c">
<apex:form>
<apex:pageBlock >
<apex:pageBlockSection id="thePageBlockSection">
<apex:inputField value="{!Text__c.Name}"/>
<apex:actionRegion >
<apex:inputField value="{!Text__c.Text_values__c}" > 
<apex:actionSupport event="onchange" reRender="as,bs,cs,ds" />
</apex:inputField>
</apex:actionRegion>
 <apex:outputPanel id="ds">
<apex:inputField  id ="as" value="{!Text__c.Text_1__c}" required="{!(Text__c.Text_values__c== '1')}"/>
<apex:inputField  id ="bs" value="{!Text__c.Text_2__c}" required="{!(Text__c.Text_values__c== '2')}"/> 
<apex:inputField   id ="cs" value="{!Text__c.Text_3__c}" required="{!(Text__c.Text_values__c== '3')}"/> 
</apex:outputPanel >
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
 
sfdotcomsfdotcom
Hi Hitesh,
I have created a record but i am not able to save it without giving "Text 1". "Text1" value becomes required even If i am not selecting any value in Text_Values__c.