Sub topic 14
When to Use Base Record Form Components Instead
This chapter assumes you can read and update records with getRecord and updateRecord, and that you know the @api recordId pattern. It helps you decide when that code is worth writing and when a base component does the job for you.
What the base components do
Lightning base components include three record forms that run on Lightning Data Service (LDS), the same layer that backs lightning/uiRecordApi.
lightning-record-view-formdisplays fields as read-only values.lightning-record-edit-formshows editable fields and saves them.lightning-record-formbuilds a whole form from a field list or a layout.
They fetch the record, apply field-level security and labels, format values for the user's locale, validate input, and save. They share the same cache, so wired getRecord results on the page update after a save.
The same feature, without JavaScript
The AccountRename component from the previous chapter needs an input handler, a field object, a try/catch block, and toast code. A record edit form does the same work in markup.
<template>
<lightning-record-edit-form
record-id={recordId}
object-api-name="Account"
onsuccess={handleSuccess}
>
<lightning-messages></lightning-messages>
<lightning-input-field field-name="Name"></lightning-input-field>
<lightning-input-field field-name="Industry"></lightning-input-field>
<lightning-button type="submit" label="Save"></lightning-button>
</lightning-record-edit-form>
</template>import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class AccountEditForm extends LightningElement {
@api recordId;
handleSuccess() {
this.dispatchEvent(
new ShowToastEvent({ title: 'Success', message: 'Account updated', variant: 'success' })
);
}
}lightning-messages displays save errors, so you write no error handling. The form also shows the field labels and picklist values without extra code.
Choosing an approach
| Need | Better choice |
|---|---|
| Standard view or edit form for one record | Base record form component |
| Quick form that follows the page layout | `lightning-record-form` |
| Custom layout, but standard fields and saving | `lightning-record-edit-form` with `lightning-input-field` |
| Show values inside your own markup, such as a header or chart | `getRecord` with `getFieldValue` |
| Save from a button, a flow of steps, or after custom logic | `updateRecord` |
| Computed values, conditional display, or data from several records | `getRecord`, `getRecords`, or `getRecordUi` |
The usual rule: start with a base component and move to the wire adapters when you need control the form cannot give you.
Common mistakes
- Writing a wire and
updateRecordcode for a plain edit form. This adds code you must test and maintain. - Using
lightning-record-formwhen you need to control field order, grouping, or buttons. Switch tolightning-record-edit-form. - Forgetting
object-api-name. Without it the form cannot find the field metadata. - Mixing both approaches on the same data without a reason. Both use LDS, so they stay in sync, but two sources of truth make the component harder to follow.
Exercise
A component must show an account's name in a custom banner with its own styling, and a separate panel must let users edit Name and Industry with the standard validation and error messages. Which approach fits each part?
Show answer
Use getRecord with getFieldValue for the banner, because you control the markup. Use lightning-record-edit-form with lightning-input-field for the edit panel, because it provides validation, error display, and saving with no extra JavaScript. Both share the LDS cache, so the banner updates after a save.