16

The lightning:recordEditForm documentation provides this example:

<aura:component>
    <lightning:recordEditForm recordId="003XXXXXXXXXXXXXXX" objectApiName="Contact">
        <lightning:messages />
        <lightning:inputField fieldName="FirstName" />
        <lightning:inputField fieldName="LastName" />
        <lightning:inputField fieldName="Email" />
        <lightning:button class="slds-m-top_small" variant="brand" type="submit" name="update" label="Update" />
    </lightning:recordEditForm>
</aura:component>

where a a click on the nested submit button causes the data to be saved.

Anyone know of a reliable/documented way to get the lightning:recordEditForm to save based on a click of a button located elsewhere in the page?

20

You can manually trigger the submit action of lightning:recordEditForm component without having to use <lightning:button type="button"/> which can be found in the new component documentation UI : https://<myDomain>/componentReference/suite.app

enter image description here

Sample code:

<lightning:recordEditForm aura:id="editForm" recordId="0032800001DYoc0" objectApiName="Contact">
      <lightning:messages />
      <lightning:inputField fieldName="FirstName" />
      <lightning:inputField fieldName="LastName" />
      <lightning:inputField fieldName="Email" />
</lightning:recordEditForm>
<lightning:button class="slds-m-top_small" variant="brand" type="button" name="update" label="update" onclick="{!c.update}" />

controller:

update : function(component,event,helper) {
   component.find("editForm").submit();
}
  • 13
    While this works for submitting the form, I did notice that using this approach does not fire the onsubmit handler of the form if you have one set. – Antonio Manente Mar 26 '18 at 20:02
4

This is working as designed. onsubmit belongs to the form (or more specifically the button in the form).

The assumption is, if you are already outside of the form, i.e. you know that another button/link is going to submit the form, you can just do whatever processing you need before firing the submit method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.