Home / lwc / lwc-lightning-record-forms
lwc-lightning-record-forms
lightning-record-form vs record-edit-form vs uiRecordApi for record create/edit in LWC. Triggers: lightning-record-form, record-edit-form, uiRecordApi form. NOT for fully custom forms — use lwc/lwc-forms-and-validation.
Open the skill folder on GitHub
LWC Lightning Record Forms
Lightning Data Service ships three form base components that remove the most common reasons developers hand-write forms: field-level security enforcement, page-layout awareness, automatic validation, and managed save/error UI. They are lightning-record-form (the all-in-one), lightning-record-edit-form (create/edit with custom layout), and lightning-record-view-form (read-only with custom layout).
The decision is mostly about how much layout control you need. lightning-record-form is one tag, takes an object + optional record Id + a layout-type, and renders the entire form including mode toggle, save/cancel buttons, spinner, and inline errors. The two -edit-form and -view-form variants give you back the template — you place lightning-input-field / lightning-output-field yourself and own the buttons. Both still leverage LDS for caching, FLS, and validation.
The mistakes follow a predictable pattern. Engineers reach for a hand-rolled form with @wire(getRecord) and manual DML the moment they need a single CSS tweak, abandoning every benefit of LDS. Or they try to mix lightning-record-edit-form with custom Apex @AuraEnabled saves, fighting the framework. Or they forget that lightning-input-field requires field-name to be a value imported via @salesforce/schema/Object.Field, not a string literal — and the form silently renders nothing.
Recommended Workflow
- Choose the smallest component that works. Start with
lightning-record-form. Move to-edit-formor-view-formonly when you need a custom layout, custom buttons, or conditional fields. Never start with hand-rolled. - Import field references via
@salesforce/schema. Thisis what enables compile-time validation, FLS enforcement, and safe refactoring. String-literal field names in
field-nameare accepted but lose all of the above. - Pick the layout-type deliberately.
Compactmirrors thecompact layout (Salesforce-managed, admin-editable).
Fullmirrors the page layout. Afields=[...]array is fully programmatic — use this when the form's contents must not shift when an admin edits the layout. - Wire
onsuccess,onsubmit, andonerror.onsubmitfires before save, lets you mutate
event.detail.fieldsfor conditional defaults;onsuccessfires after, lets you fire a toast or navigate. Withoutonerror, validation errors still show inline (LDS handles it) but you cannot react programmatically. - **Set
density="comfy" | "compact" | "auto"to match the hostsurface.** Default
autoadapts to the SLDS density flag set on the user record — overriding only makes sense when the form is on a tightly-scoped page (a modal) where you know which layout you want. - Test by simulating LDS in jest. Use
@salesforce/sfdx-lwc-jestmocks forgetRecord/createRecord/updateRecord. Do not unit-test the form's internal save logic — that is platform code.
When To Reach For Custom (Skip This Skill)
If you need a wizard with multiple steps, a custom save endpoint that does cross-object orchestration, or a UI tightly coupled to non-Salesforce data, do not start with lightning-record-edit-form. Build the form with lightning-input components and call your Apex method directly. The LDS form components are for the case where Salesforce is the source of truth for one record at a time.
What This Skill Does Not Cover
| Topic | See instead |
|---|---|
Fully custom forms with uiRecordApi | lwc/lwc-custom-form-with-uiRecordApi |
| Custom lookup fields inside a form | lwc/lwc-custom-lookup |
| Datatable inline edit | lwc/lwc-datatable-advanced |
Aura lightning:recordEditForm | Aura is deprecated for new work |