You need to sign in to do that
Don't have an account?
Rohit Shisode
input field values getting lost when calling controller method from action function that is called from js
I have contact fields that i need to validate on client side and if everything is ok then insert that contact. But when i go into js for validation and call controller method from js using action fucntion all my inputField values are null. Contact does not get values that i have written, Please help!
VF:
Js
Controller method
VF:
<apex:pageBlock rendered="{!IsPkg}" id="contactPageBlock"> <apex:pageBlockSection title="Guest Registration" columns="1" collapsible="false" id="contactSection"> <apex:inputField value="{!contactObj.FirstName}" label="First Name" id="fname"/> <apex:inputField value="{!contactObj.LastName}" label="Last Name" id="lname"/> <apex:inputField value="{!contactObj.Email}" label="Email" id="cmail"/> </apex:pageBlockSection> <apex:commandButton onclick="validate();return false;" value="Save"/> <apex:actionfunction name="callfromJS" action="{!saveContact}" immediate="true"/> </apex:pageBlock>
Js
function validate(){ try{ var fNameObj = document.getElementById("{!$Component.form.contactPageBlock.contactSection.fname}"); var lNameObj = document.getElementById("{!$Component.form.contactPageBlock.contactSection.lname}"); var eNameObj = document.getElementById("{!$Component.form.contactPageBlock.contactSection.cmail}"); console.log(fNameObj.value); console.log(lNameObj.value); console.log(eNameObj.value); if(trim(fNameObj.value) == "" || trim(lNameObj.value) == "" || trim(eNameObj.value) == ""){ console.log("Inside If"); window.alert("All fields are mandatory!"); return false; } else if(trim(eNameObj.value) != ""){ var x = eNameObj.value; var atpos = x.indexOf("@"); var dotpos = x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { window.alert("Not a valid e-mail address"); return false; }else { var r = confirm("Register a new guest?"); if(r==true){ callfromJS(); return true; }else{ return false; } } } } catch(e){ console.log("Inside Catch"); } } </script>
Controller method
public void saveContact(){ System.debug('Inside Save'); System.debug('Contact Constructed is::'+contactObj); System.debug('selectedEventId:::'+selectedEventId); System.debug('selectedPackageId:::'+selectedPackageId); if(String.isNotBlank(selectedPackageId)){ System.debug('Contact Constructed is::'+contactObj); if(contactObj.LastName != null){ System.debug('Contact Constructed is::'+contactObj); contactObj.Package__c = selectedPackageId; try{ System.debug('Contact Constructed is::'+contactObj); //insert contactObj; } catch(Exception e){ } } } }
The attribute "immediate" in your actionFunction is set to True. When it is set to True, values are not passed to the controller automatically. Try removing the "immediate" attribute or you will have to use apex:param inside the actionFunction to pass the controller the data.
Hope that helps
All Answers
The attribute "immediate" in your actionFunction is set to True. When it is set to True, values are not passed to the controller automatically. Try removing the "immediate" attribute or you will have to use apex:param inside the actionFunction to pass the controller the data.
Hope that helps