4

The lightning-record-edit-form has a documented submit method that I assume can produce similar behavior to a click on a nested submit button. But I can't find any examples of this.

The documentation describes a fields argument for the submit method of:

Submits the form using an array of record fields or field IDs. The field ID is provisioned from @salesforce/schema/. Invoke this method only after the load event.

When no argument is supplied to submit, features like required field validation does not work. I'm not in a position to test right now so am wondering:

  • If the fields are hard-coded as the submit argument, does the expected behavior result?
  • Can the field names specified in the nested lightning-input-field elements be programmatically obtained? (I note that lightning-record-edit-form has a field-names property but that is documented as "Reserved for internal use. Names of the fields to include in the form.".)
2
  • Check the last section Overriding Default Behaviors, that first code example in aura recordEditForm docs. fields seems to be the event parameter that contains all the fields Commented Feb 5, 2020 at 22:03
  • It's incredibly frustrating that we're provided with the submit method to call from outside of lightning-record-edit-form but the behavior is inconsistent with including an input or a button inside of the component. 1) the field validations do not fire and 2) if you have a submit handler (onsubmit) it isn't called! Additionally, there's no mechanism to grab the fields from a lightning-record-edit-form unless you use a querySelector or the fields are set dynamically from the JS side of the component.
    – nbrown
    Commented Apr 2, 2021 at 12:45

2 Answers 2

12

I have run into this often since moving from Aura to lightning web components (LWC) and have found that the only solution is to hide a submit button on the form, and then call click on it from your JS method when desired.

A very basic example of this would look like this:

HTML

<template>
   <lightning-spinner if:false={rendered} 
                      alternative-text="loading..." 
                      variant="brand" 
                     size="small" >
   </lightning-spinner>
   <template if:true={rendered}>
       <lightning-record-edit-form onsubmit={handleSubmit}
                                   object-api-name="Account" >
            <lightning-messages></lightning-messages>
            <lightning-input-field field-name="Name" ></lightning-input-field>
            <input type="submit" class="hidden" />
        </lightning-record-edit-form>
        <lightning-button onclick={submitMyformProgramatically} 
                          variant="brand" 
                          label="Click me" >
         </lightning-button>
    </template>
</template>

JS

import { LightningElement, track} from 'lwc';

export default class App extends LightningElement {

  @track 
  rendered = false;

  connectedCallback(){
     this.rendered = true;
  }

  submitMyformProgramatically(){
     const btn = this.template.querySelector( ".hidden" );

     if( btn ){ 
        btn.click();
     }
  }

  handleFormSubmit( event ){
     const fields = event.detail.fields;
     console.log( "Fields: ", fields );
  }

}

CSS

.hidden {
    display: none;
}

in this example, a simple click of the external button will submit this form programatically by locating the button on the form and clicking it.

In Aura lightning:recordEditform, you used to be able to set an aura:id on the form and simply call submit from your JS, but that doesn't work in the new LWC lightning-record-edit-form

4
  • 1
    Thanks Ronnie, I didn't want to resort to that if there was a cleaner but more obscure way to do it.
    – Keith C
    Commented Feb 6, 2020 at 8:18
  • 1
    I looked for something documented, found nothing. I too wanted a different way.
    – Ronnie
    Commented Feb 6, 2020 at 13:00
  • 2
    Nested button it shall be...
    – Keith C
    Commented Feb 6, 2020 at 17:51
  • 2
    worked perfectly, I just found in time; I was about to spend too much effort in rebuilding the form
    – GirishP
    Commented Sep 15, 2020 at 2:35
2

You can call the submit method of the record-edit-form component directly. It is much cleaner than hiding a button.

this.template.querySelector('lightning-record-edit-form').submit();
2
  • In my testing, that was not directly equivalent... but I can't remember what the difference was.
    – Keith C
    Commented Dec 1, 2020 at 21:04
  • 5
    Trouble with this approach is that it does not trigger the onsubmit event.
    – Dane
    Commented Jan 27, 2021 at 10:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .