Home > visualforce > Clear a Default Fields Value After Reset

Clear a Default Fields Value After Reset

April 21Hits:2

I am trying to clear a set of field's values on the click of reset button. I am using the below given code in my application

<apex:page standardController="Account" extensions="Sample"> <apex:form id="pageForm">     <apex:pageBlock title="My Content" id="thePageBlock" mode="edit">         <apex:pageBlockButtons>             <apex:commandButton action="{!save}" value="Save"/>                             <apex:commandButton onclick="clearFieldValues(); return false;"                   value="Reset"/>         </apex:pageBlockButtons>         <apex:pageBlockSection title="Customer Details" id="pageBlockSection" columns="2">             <apex:inputField value="{!account.name}" id="name" required="true"/>             <apex:inputField value="{!account.site}" id="site" required="true"/>             <apex:inputField value="{!account.type}" id="accType"/>             <apex:inputField value="{!account.accountNumber}" id="accNo"/>         </apex:pageBlockSection>     </apex:pageBlock>      <Script>     function clearFieldValues(){     document.getElementById("{!$Component.pageForm.thePageBlock.pageBlockSection.name}").value="";     document.getElementById("{!$Component.pageForm.thePageBlock.pageBlockSection.site}").value="";     document.getElementById("{!$Component.pageForm.thePageBlock.pageBlockSection.accType}").value="";     document.getElementById("{!$Component.pageForm.thePageBlock.pageBlockSection.accNo}").value="";     }     </Script>  </apex:form> 

And then the code i have used in my apex class is as given below.

public without Sharing class Sample { public Sample() { } public PageReference reset() {     PageReference pg = new PageReference(System.currentPageReference().getURL());     pg.setRedirect(false);     return pg; }} 

The above code clears most of the fields. But fields that are marked required are not getting cleared. They are being populated with the values that were selected before i hit the reset button. Is there something that I am doing wrong here.


I think you should add the immediate="true" to your apex:commandButton and in this case I think you don't need an apex class for this

Salesforce Documentation

You need to setRedirect to true to flush the view state of your page. From the documentation for setRedirect:

This type of redirect performs an HTTP GET request, and flushes the view state, which uses POST. If set to false, the redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.

Alternatively you could add a:

account = new Account();

to your reset() method.

EDIT: I just noticed you aren't actually using the reset method in your controller, you're just calling Javascript. If you want an action on the commandButton for reset, you'd need to add action="{!reset}", which I think would work as an alternative to your javascript clearFieldValues function.

Related Articles

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.752 s.