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
sfg1sfg1 

Field readonly after clicking save button in visualforce page

I have long text area Text field 'comments__c', after entering data in comments__c text box and click on save, comment field should be read only I tried below code, but it is not working as expected. I need to display that page in SITE.please guide me.

Controller:
public class CampaignRecordIdController{
    public String campaignRecordId {get;set;}
    public String parameterValue {get;set;}
    public Campaign cam{get;set;}
    public boolean rend{get;set;}
    public boolean rend1{get;set;}
    public Campaign Campaign{get;set;}
    
    public CampaignRecordIdController() 
    {
       campaignRecordId =ApexPages.currentPage().getParameters().get('id');
       system.debug('=====campaignRecordId ===='+campaignRecordId);
       cam = [select name,Promotion_Details__c,parentid,Comments__C from Campaign member where id =: campaignRecordId ];
       rend=true;
       rend1=false;
      
        }
         public PageReference save() {
        update cam;
        rend=false;
        rend1=true;
        return null;
    }
}

Page:
<apex:page Controller="CampaignRecordIdController"  sidebar="false" showheader="false">
   <apex:form >
   <apex:pageBlock>
    
   <apex:pageBlockSection title="Current Campaign record Id is : {!campaignRecordId}" collapsible="false" columns="1" rendered="{!rend}">
   <apex:outputField value="{!cam.name}"/>
   <apex:outputField value="{!cam.Promotion_Details__c}"/>  
   <apex:outputField value="{!cam.Parentid}"/>  
   <apex:inputtext value="{!cam.Comments__c}" style="width: 360px; height: 40px"/>  
   </apex:pageBlockSection> 
   
    <apex:pageBlockSection title="Current Campaign record Id is : {!campaignRecordId}" collapsible="false" columns="1" rendered="{!rend1}">
   <apex:outputField value="{!cam.name}"/>
   <apex:outputField value="{!cam.Promotion_Details__c}"/>  
   <apex:outputField value="{!cam.Parentid}"/>  
   <apex:outputfield value="{!cam.Comments__c}" style="width: 360px; height: 40px"/>  
   </apex:pageBlockSection> 
   
     <apex:pageBlockButtons >
      <apex:commandButton action="{!save}" value="save"/>
     </apex:pageBlockButtons>
    </apex:pageBlock>
   </apex:form>
   </apex:page>

 
Best Answer chosen by sfg1
Balayesu ChilakalapudiBalayesu Chilakalapudi
Add this condition in constructor
if(cam.Comments__C.length!=0 && status=="Deny"){
            disabled=true;
 }
Add some javascript logic to do validation, like this
<apex:inputfield value="{!cam.Comments__c}" disabled="{!disabled} id="check" style="width: 360px; height: 40px"/> 
<div id="err" style="color:red" />
​<apex:commandButton action="{!save}" value="save" onClick="return validate()" />
<script type="text/javascript">
function validate(){
   var inputvalue=document.getElementById("j_id0:j_id1:check").value;
   if(inputvalue=""){
       document.getElementById("j_id0:j_id1:err").innerHTML="Field is required";
       return false;
   }
   else
      document.getElementById("j_id0:j_id1:err").innerHTML="";
}
</script>
Let us know if it helps you.

All Answers

LBKLBK
Yout need to reRender these two apex:PageBlockSections for your Boolean change to take effect.

Try this code.
 
<apex:page Controller="CampaignRecordIdController"  sidebar="false" showheader="false">
   <apex:form >
   <apex:pageBlock>
    
   <apex:pageBlockSection id="EditBlock" title="Current Campaign record Id is : {!campaignRecordId}" collapsible="false" columns="1" rendered="{!rend}">
   <apex:outputField value="{!cam.name}"/>
   <apex:outputField value="{!cam.Promotion_Details__c}"/>  
   <apex:outputField value="{!cam.Parentid}"/>  
   <apex:inputtext value="{!cam.Comments__c}" style="width: 360px; height: 40px"/>  
   </apex:pageBlockSection> 
   
    <apex:pageBlockSection id="ViewBlock"  title="Current Campaign record Id is : {!campaignRecordId}" collapsible="false" columns="1" rendered="{!rend1}">
   <apex:outputField value="{!cam.name}"/>
   <apex:outputField value="{!cam.Promotion_Details__c}"/>  
   <apex:outputField value="{!cam.Parentid}"/>  
   <apex:outputfield value="{!cam.Comments__c}" style="width: 360px; height: 40px"/>  
   </apex:pageBlockSection> 
   
     <apex:pageBlockButtons >
      <apex:commandButton action="{!save}" value="save" reRender="EditBlock, ViewBlock"  />
     </apex:pageBlockButtons>
    </apex:pageBlock>
   </apex:form>
   </apex:page>
Hope this helps.
 
Balayesu ChilakalapudiBalayesu Chilakalapudi
Try using disabled attribute like this,
 <apex:inputtext value="{!cam.Comments__c}" disabled="{!rend1}" style="width: 360px; height: 40px"/>  

 
sfg1sfg1
Bala, it is working, but if i copy the same URL  and paste it, again it is showing the text box which is editable.Please guide me in this. Also i tried making 'comment__c' field mandotory using "required="true" but i am not seeing red color mark. pls guide me.
Balayesu ChilakalapudiBalayesu Chilakalapudi
Use css style to show red color mark, use another variable for disabled like this,
  
public boolean disabled{get;set;}    
    public CampaignRecordIdController() 
    {
       disabled=false;
       campaignRecordId =ApexPages.currentPage().getParameters().get('id');
       system.debug('=====campaignRecordId ===='+campaignRecordId);
       cam = [select name,Promotion_Details__c,parentid,Comments__C from Campaign member where id =: campaignRecordId ];
       rend=true;
       rend1=false;
        if(cam.Comments__C.length>2){
            disabled=true;
        }      
    }
sfg1sfg1
Thanks for reply Bala, i have acheived red color mark using   
   <div class="requiredinput">
   <div class="requiredBlock"></div> 
   <apex:inputfield value="{!cam.Comments__c}" required="true" style="width: 360px; height: 40px"/> 
   </div>
Now i need to check condition, if 'status=Deny' in 'Campaignmember', second time i should not allow them to enter comments in field(Comments--C) . It sholud through error like 'Already comments entered'. I tried for checking condition in contstructor, but i am getting error.pls guide me.
Class 
public class CampaignRecordIdController{
    public String campaignRecordId {get;set;}
    public campaignmember cam{get;set;}
    public boolean rend{get;set;}
    public boolean rend1{get;set;}
    public boolean disabled{get;set;}   
    public string Campaignmemberid {get;set;}
    public CampaignRecordIdController() 
    {
    campaignRecordId = System.currentPageReference().getParameters().get('id');
    system.debug('=====campaignRecordId ===='+campaignRecordId);
    Cam = [select name,Promotion_Details__c,Comments__C,campaignid from Campaignmember where id =: campaignRecordId ];
    rend=true;
    rend1=false;
    }
    public PageReference save() {
    update cam;
    rend=false;
    rend1=true;
    return null;
    }
    }
Page
<apex:page Controller="CampaignRecordIdController"  sidebar="false" showheader="false">
   <apex:form >
   <apex:pageBlock >
   <apex:pageBlockSection title="Current Campaign record Id is : {!campaignRecordId}" collapsible="false" columns="1" rendered="{!rend}">
   <apex:outputField value="{!cam.name}"/>
   <apex:outputField value="{!cam.Promotion_Details__c}"/>  
   <apex:outputField value="{!cam.campaignid}"/> 
   <div class="requiredinput">
   <div class="requiredBlock"></div> 
   <apex:inputfield value="{!cam.Comments__c}" required="true" style="width: 360px; height: 40px"/> 
   </div>
   </apex:pageBlockSection>
   <apex:pageBlockSection title="Current Campaign record Id is : {!campaignRecordId}" collapsible="false" columns="1" rendered="{!rend1}">
   <apex:outputField value="{!cam.name}"/>
   <apex:outputField value="{!cam.Promotion_Details__c}"/>  
   <apex:outputField value="{!cam.campaignid}"/>  
   <apex:outputfield value="{!cam.Comments__c}" style="width: 360px; height: 40px"/>  
   </apex:pageBlockSection> 
   <apex:pageBlockButtons >
   <apex:commandButton action="{!save}" value="save"/>
   </apex:pageBlockButtons>
   </apex:pageBlock>
   </apex:form>
   </apex:page>
Balayesu ChilakalapudiBalayesu Chilakalapudi
Add this condition in constructor
if(cam.Comments__C.length!=0 && status=="Deny"){
            disabled=true;
 }
Add some javascript logic to do validation, like this
<apex:inputfield value="{!cam.Comments__c}" disabled="{!disabled} id="check" style="width: 360px; height: 40px"/> 
<div id="err" style="color:red" />
​<apex:commandButton action="{!save}" value="save" onClick="return validate()" />
<script type="text/javascript">
function validate(){
   var inputvalue=document.getElementById("j_id0:j_id1:check").value;
   if(inputvalue=""){
       document.getElementById("j_id0:j_id1:err").innerHTML="Field is required";
       return false;
   }
   else
      document.getElementById("j_id0:j_id1:err").innerHTML="";
}
</script>
Let us know if it helps you.
This was selected as the best answer