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
up_skyup_sky 

how to keep value on the fields after submit form

Hi All,

 

   

I created a VF page with required input fields and a validation rule along with a command buttons ( "Update" buttons). While submitting the information if I does not input on one of the required fields, It displays the validation rule error message and at the same time all fields are get cleared.

 

But, I like to keep all data on the fields, but display error message.


How to resolve this "Fields get cleared" problem... Please suggest me

 

Thanks,

 

 

 

<apex:page Controller="MyAccount">
    <apex:form >    
    <apex:outputPanel id="AuthorizedContacts">    
    <apex:message/>
    <h2>{!primaryContact.Name}</h2><br/>
    <apex:commandbutton action="{!addAuthContact}" value="Add Contact" rerender="AuthorizedContacts" rendered="{!addAuthContact == false}"/>
    
        <apex:repeat value="{!newAuth}" var="a">
        <apex:outputpanel rendered="{!addAuthContact}">
            {!a.Contact_Type__c}<br/>
            <h4>Name &amp; Contact</h4><br/>
              
                FIRST NAME* <apex:inputField value="{!a.FirstName}" id="contact-first-name" required="true"/>
                LAST NAME* <apex:inputField value="{!a.LastName}"  id="contact-last-name" required="true"/>
                PHONE* <apex:inputField value="{!a.Phone}"  id="contact-phone" required="true"/>
                EMAIL ADDRESS* <apex:inputField value="{!a.Email}"  id="contact-email" required="true"/> 
                
            <h4>Contact Address</h4><br/>
            
                ADDRESS 1 <br/><apex:inputText value="{!a.Contact_Address_Line_1__c}"  id="contact-addr-1"/><br/>
                ADDRESS 2 <br/><apex:inputText value="{!a.Contact_Address_Line_2__c}"  id="contact-addr-2"/><br/>
                CITY <br/><apex:inputText value="{!a.Contact_Town_City__c}"  id="contact-city"/><br/>
                POST CODE <br/><apex:inputText value="{!a.Contact_Postal_Code__c}"  id="contact-postal"/><br/>
                COUNTY <br/><apex:inputText value="{!a.Contact_County__c}"  id="County"/><br/>
                
                COUNTRY<br/>
                <apex:SelectList value="{!CountryName}" size="1">
                    <apex:selectOptions value="{!lstCountry}"></apex:selectOptions>
                </apex:SelectList><br/>
            
            <h4>Is This Authorized Contact Part of Your:</h4><br/>
                <apex:Inputcheckbox value="{!a.IsHousehold__c}"/>
                <label>Household</label>
                <apex:Inputcheckbox value="{!a.IsBusiness__c}"/>
                <label >Business</label><br/>
                <apex:messages rendered="{!IsValidate}" styleClass="errorMsg"/><br/>
            <apex:commandbutton styleClass="cancel" action="{!cancelAuth}" value="Cancel" immediate="true" rerender="AuthorizedContacts"/>
            <apex:commandButton styleClass="button-medium" action="{!saveAuth}" value="Save Updates" rerender="AuthorizedContacts"/>
        
          </apex:outputPanel>      
        </apex:repeat>
        </apex:outputPanel>
    </apex:form>
</apex:page>

 

 

public class MyAccount {
    
    private Id uid;
    public Id aid{get;set;}
    public Id cid{get;set;}
	
	public string CountryName {get;set;}
	
    public Boolean IsValidate{get;set;}
    public Boolean addAuthContact{get;set;}
    
    public User objUser{get;set;} 
    public Contact primaryContact;
	
    public list<Contact> newAuth;
	
    public MyAccount(){
	
        uid = UserInfo.getUserId();
        getObjUser();    
    }
    
    public List<SelectOption> getlstCountry()
    {  list<SelectOption> options = new list<SelectOption>(); 
        try{
            list<Country__c> ls = [select Name,LZ_Country_Id__c,Id from Country__c];
            
			 	Country__c uk = [select Name,LZ_Country_Id__c,Id from Country__c Where Name = 'United Kingdom'];
            
			 	options.add(new SelectOption(uk.Id,uk.Name));
				
            for(Country__c o:ls){
                if(o.Name != 'United Kingdom')
                options.add(new SelectOption(o.Id,o.Name));
            }
        }
        catch(Exception ex)
        {
            System.debug('Error From ' + ex.getMessage());
        }  
        return options;
    }
	
    public void getObjUser(){
        try {
            uid = Apexpages.currentPage().getParameters().get('id');
            if(uid != null){
            objUser = [Select   User.ContactId,
                                User.AccountId,
                                User.UserType
                        From    User
                        Where   User.Id = :uid ];
            cid = objUser.ContactId;
            aid = objUser.AccountId;    
            }
        }
        catch(Exception ex)
        {
            System.debug('Error From ' + ex.getMessage());
        } 
    }
    
    public Contact getprimaryContact(){
        try
        {
            if(cid != null){
                primaryContact = [Select    Contact.Id,
                                            Contact.Name,
                                    From    Contact
                                    Where   Contact.Id =:cid];
            }
        }catch(Exception ex){
            System.debug('Error From ' + ex.getMessage());
        } 
        return primaryContact;
    }    
  
    public list<Contact> getnewAuth(){
        try
            {   
                newAuth = new list<Contact>();
                Contact ac = new Contact();
                if(aid != null) {
                    ac.AccountId = aid;
                    ac.Contact_Type__c = 'Authorized Contact';
                } 
                newAuth.add(ac);    
        }
        catch(Exception ex)
        {
                System.debug('Error From' + ex.getMessage());
        }   
         return newAuth;
    }
    
    public PageReference addAuthContact(){
        try{
            getnewAuth();
            addAuthContact = true;
        }
        catch(Exception ex)
        {
            System.debug('Error From ' + ex.getMessage());
        }
        return null;
    }   
     
    public PageReference cancelAuth(){
        try{
            newAuth = null;
            addAuthContact = false;
        }
        catch(Exception ex)
        {
            System.debug('Error From ' + ex.getMessage());
        }
        return null;
    }
    
    public PageReference saveAuth() {  
        
         try{
        List<Contact> tempCon = new List<Contact>();
        if(newAuth != null)  {      
            for(Contact t : newAuth)
            {
                t.Contact_Country__c = CountryName ;                
                tempCon.add(t);
            }
        }
        insert tempCon;
        addAuthContact = false;
        }catch(DmlException ex){
        	if(newAuth[0].IsBusiness__c == false && newAuth[0].IsHousehold__c == false){
        		IsValidate = true;
        	}        	
            ApexPages.addMessages(ex);
        }
        return null;
    }
}

 

 

 

Arun MKArun MK

Hi,

 

Instead of re-rendering the outputPanel, try to re-render the apex:message tag.

 

 

<apex:page Controller="MyAccount">
<apex:form >
<apex:outputPanel id="AuthorizedContacts">
<apex:message id="msgId/>

....

....

....
<apex:messages rendered="{!IsValidate}" styleClass="errorMsg" id="msgsId"/><br/>
<apex:commandbutton styleClass="cancel" action="{!cancelAuth}" value="Cancel" immediate="true" rerender="AuthorizedContacts"/>
<apex:commandButton styleClass="button-medium" action="{!saveAuth}" value="Save Updates" rerender="msgId,msgsId"/>

</apex:outputPanel>
</apex:repeat>
</apex:outputPanel>
</apex:form>
</apex:page>

 

Regards,

Arun.

up_skyup_sky

Hi 

I have tried this but didn't work it not clear the value but my error message doesn't appear

 

<apex:page Controller="ContactsCls" sidebar="false" showHeader="false">
    <apex:form >    
    <apex:outputPanel id="AuthorizedContacts">
<apex:message id="msgId"/>
<h2>{!primaryContact.Name}</h2><br/>
<apex:commandbutton action="{!addAuthContact}" value="Add Contact" rerender="AuthorizedContacts" rendered="{!addAuthContact == false}"/> <apex:repeat value="{!newAuth}" var="a"> <apex:outputpanel rendered="{!addAuthContact}"> {!a.Contact_Type__c}<br/> <h4>Name &amp; Contact</h4><br/> FIRST NAME* <apex:inputField value="{!a.FirstName}" required="true"/> LAST NAME* <apex:inputField value="{!a.LastName}" required="true"/> PHONE* <apex:inputField value="{!a.Phone}" required="true"/> EMAIL ADDRESS* <apex:inputField value="{!a.Email}" required="true"/> <h4>Contact Address</h4><br/> ADDRESS 1 <br/><apex:inputText value="{!a.Contact_Address_Line_1__c}"/><br/> ADDRESS 2 <br/><apex:inputText value="{!a.Contact_Address_Line_2__c}"/><br/> CITY <br/><apex:inputText value="{!a.Contact_Town_City__c}"/><br/> POST CODE <br/><apex:inputText value="{!a.Contact_Postal_Code__c}"/><br/> COUNTY <br/><apex:inputText value="{!a.Contact_County__c}"/><br/> COUNTRY<br/> <apex:SelectList value="{!CountryName}" size="1"> <apex:selectOptions value="{!lstCountry}"></apex:selectOptions> </apex:SelectList><br/> <h4>Is This Authorized Contact Part of Your:</h4><br/> <apex:Inputcheckbox value="{!a.IsHousehold__c}"/> <label>Household</label> <apex:Inputcheckbox value="{!a.IsBusiness__c}"/> <label >Business</label><br/> <apex:messages rendered="{!IsValidate}" styleClass="errorMsg" id="msgsId"/><br/> <apex:commandbutton action="{!cancelAuth}" value="Cancel" immediate="true" rerender="AuthorizedContacts"/> <apex:commandButton action="{!saveAuth}" value="Save Updates" rerender="msgId,msgsId"/> </apex:outputPanel> </apex:repeat> </apex:outputPanel> </apex:form> </apex:page>

 

Thanks,

Apple