Sub topic 10
Overriding submit to set or validate field values
This chapter assumes you can build a basic lightning-record-edit-form with lightning-input-field children and handle its onsuccess and onerror events. It also assumes you know how to read an event's detail in a handler.
Why override submit
By default, a lightning-record-edit-form saves the values the user typed as soon as they click a button with type="submit". Sometimes you need to act before that save happens. Common cases:
- Trim or normalize a value, such as removing extra spaces from a last name.
- Set a field the user does not see, such as a default status.
- Run a check that a field-level validation rule cannot express, and stop the save if it fails.
The form fires a submit event just before saving. If you handle it, you can change what gets saved or stop the save.
How the submit event works
When the user clicks the submit button, the form fires submit. The event carries the field values in event.detail.fields, an object keyed by field API name.
If you do nothing, the form saves those values. If you call event.preventDefault(), the form does not save. You can then call the form's submit(fields) method yourself with the values you want.
A working example
Bind the handler in the template with onsubmit.
<template>
<lightning-record-edit-form
object-api-name="Contact"
onsubmit={handleSubmit}
onsuccess={handleSuccess}
onerror={handleError}>
<lightning-messages></lightning-messages>
<lightning-input-field field-name="FirstName"></lightning-input-field>
<lightning-input-field field-name="LastName"></lightning-input-field>
<lightning-input-field field-name="Email"></lightning-input-field>
<lightning-button class="slds-m-top_small" type="submit" label="Save"></lightning-button>
</lightning-record-edit-form>
</template>The handler cancels the default save, cleans the value, and submits explicitly.
handleSubmit(event) {
event.preventDefault();
const fields = event.detail.fields;
fields.LastName = (fields.LastName || '').trim();
this.template
.querySelector('lightning-record-edit-form')
.submit(fields);
}The || '' guard matters. If the user leaves the field empty, the value may be null, and calling trim() on null throws an error.
Setting a value the user does not see
You can add a field to the fields object even if no lightning-input-field for it exists on the form. For example, add fields.LeadSource = 'Web'; before calling submit(fields). The user must have edit access to that field, or the save fails.
Validating before saving
To block a save, call preventDefault() and simply do not call submit. Show the user why with a toast or a message in your template.
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class ContactCreateChecked extends LightningElement {
handleSubmit(event) {
event.preventDefault();
const fields = event.detail.fields;
if (!fields.Email && !fields.Phone) {
this.dispatchEvent(
new ShowToastEvent({
title: 'Missing contact detail',
message: 'Enter an email or a phone number.',
variant: 'error'
})
);
return;
}
this.template
.querySelector('lightning-record-edit-form')
.submit(fields);
}
}This example assumes the form also includes Phone as a lightning-input-field. Fields not on the form are absent from event.detail.fields.
Common mistakes
- Forgetting
preventDefault(). Without it, the original values are saved, and your changes infieldsmay be ignored. - Calling
preventDefault()but never submitting. The button seems to do nothing. Every code path that should save must callsubmit(fields). - Looking for the wrong key. Keys in
fieldsare field API names, so custom fields end in__c. - Assuming your check replaces server rules. Validation rules, required fields and triggers still run on save. Failures arrive through
onerrorandlightning-messages.
Exercise
A developer handles onsubmit, sets event.detail.fields.Description = 'Created online', and does nothing else. The record is saved without the description. What is missing, and why?
Show answer
The handler needs event.preventDefault() followed by this.template.querySelector('lightning-record-edit-form').submit(fields). Without preventDefault(), the form continues its default save with the original values, so the modification is lost.
Exercise
In a submit handler you want to refuse the save when LastName is empty. What do you do, and what must you avoid?
Show answer
Call event.preventDefault(), show a message to the user, and return without calling submit(). Avoid calling submit() on that path, because that would save the record anyway.