Sub topic 9
Record forms with lightning-record-edit-form
This chapter assumes you can build a basic Lightning web component with an HTML template, a JavaScript class, and a CSS file. It also assumes you know how lightning-input works. Here you will use lightning-record-edit-form to create a record without writing Apex.
What the component does
lightning-record-edit-form is a base component that wraps form fields and talks to Lightning Data Service (LDS). LDS is the Salesforce layer that loads, saves, and caches records for components. It also enforces field-level security and object permissions, so you don't write that logic yourself.
You give the form an object-api-name. Inside it you place lightning-input-field elements, each with a field-name. Each field shows the right input for its field type. A date field gets a date picker, and a picklist gets a dropdown. Labels, required markers, and picklist values come from the object's metadata.
Compare this with lightning-input. That component is a generic input and knows nothing about Salesforce objects. lightning-input-field is tied to a field on an object, and it only works inside a record form.
A minimal create form
<template>
<lightning-record-edit-form object-api-name="Contact" onsuccess={handleSuccess}>
<lightning-messages></lightning-messages>
<lightning-input-field field-name="FirstName"></lightning-input-field>
<lightning-input-field field-name="LastName"></lightning-input-field>
<lightning-button
class="slds-m-top_small"
type="submit"
variant="brand"
label="Save"
></lightning-button>
</lightning-record-edit-form>
</template>Three details make this work:
- With no
record-id, the form creates a new record. With arecord-id, it edits that record. - A button with
type="submit"triggers the save. You don't need a click handler. lightning-messagesdisplays save errors and validation errors, so you don't need your own error markup.
Reacting to the result
The form fires events at different points in the save.
| Event | Fires when | Typical use |
|---|---|---|
| `onsubmit` | The user submits, before the save | Change field values or stop the save |
| `onsuccess` | The save worked | Show a toast, read the new record Id |
| `onerror` | The save failed | Log or show a custom message |
The onsuccess handler receives the saved record in event.detail:
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class ContactCreate extends LightningElement {
handleSuccess(event) {
const recordId = event.detail.id;
this.dispatchEvent(
new ShowToastEvent({
title: 'Contact created',
message: 'Record Id: ' + recordId,
variant: 'success'
})
);
}
}Changing values before saving
Use onsubmit when you need to adjust data. Call preventDefault() to stop the default save, then submit the form yourself with your changes:
handleSubmit(event) {
event.preventDefault();
const fields = event.detail.fields;
fields.LastName = fields.LastName.trim();
this.refs.form.submit(fields);
}This needs lwc:ref="form" on the form element in the template. If you skip preventDefault(), the form saves the original values.
How the pieces connect
Common errors
- Fields outside the form. A
lightning-input-fieldplaced outsidelightning-record-edit-formdoes not render correctly. Keep every field inside the form. - Button without
type="submit". A plainlightning-buttondoes nothing on click. Settype="submit". - Wrong field names. Use API names, such as
FirstNameorCustom_Field__c. A wrong name leaves the field blank. - Missing field access. If the user can't see a field, it won't render. Check field-level security before you suspect your code.
Exercise
A developer adds a lightning-button with label="Save" inside a lightning-record-edit-form. Clicking it does not save the record. What is the most likely fix, and how would the developer read the new record's Id after a successful save?
Show answer
Add type="submit" to the button so it triggers the form submission. To read the Id, add an onsuccess handler on the form and use event.detail.id.