Lightning Data Service · Lesson 15 of 25
Record Forms in LWC: lightning-record-form and Edit Form
For viewing and editing a record, you often don't need to build a form at all. Three base components – lightning-record-form, lightning-record-edit-form and lightning-record-view-form – render fields with the right input type, labels, help text, validation and field-level security, and save through Lightning Data Service. Knowing which one to pick saves hours.
In this lesson
lightning-record-form: the fastest optionlightning-record-edit-form: your layout, your buttons- Default values and custom logic on submit
- Choosing between the three
lightning-record-form
<!-- quickAccountForm.html -->
<template>
<lightning-card title="Account details">
<lightning-record-form
record-id={recordId}
object-api-name="Account"
fields={fields}
columns="2"
mode="view"
onsuccess={handleSuccess}>
</lightning-record-form>
</lightning-card>
</template>
// quickAccountForm.js
import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import NAME from '@salesforce/schema/Account.Name';
import PHONE from '@salesforce/schema/Account.Phone';
import INDUSTRY from '@salesforce/schema/Account.Industry';
import REVENUE from '@salesforce/schema/Account.AnnualRevenue';
export default class QuickAccountForm extends LightningElement {
@api recordId;
fields = [NAME, PHONE, INDUSTRY, REVENUE];
handleSuccess() {
this.dispatchEvent(new ShowToastEvent({ title: 'Account saved', variant: 'success' }));
}
}
▌Account details [record-form: Account 001XX000003DGb2 mode=view fields: Name, Phone, Industry, AnnualRevenue]
| Mode | Behaviour |
|---|---|
view | read-only, with pencil icons to switch a field to edit |
edit | editable, with Save and Cancel |
readonly | read-only, no editing at all |
Without record-id it becomes a create form. Instead of fields you can pass layout-type="Full" or "Compact" to use the object's page layout.
lightning-record-edit-form: control the layout
When you need your own layout, extra content between fields, custom buttons or logic before saving, use the edit form with lightning-input-field for each field:
<!-- newCaseForm.html -->
<template>
<lightning-record-edit-form object-api-name="Case" onsubmit={handleSubmit} onsuccess={handleSuccess} onerror={handleError}>
<lightning-messages></lightning-messages>
<lightning-input-field field-name="Subject" required></lightning-input-field>
<lightning-input-field field-name="Priority" value="Medium"></lightning-input-field>
<lightning-input-field field-name="Description"></lightning-input-field>
<lightning-button type="submit" variant="brand" label="Create case"></lightning-button>
</lightning-record-edit-form>
</template>
// newCaseForm.js
import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class NewCaseForm extends LightningElement {
@api accountId;
handleSubmit(event) {
event.preventDefault(); // stop the default save
const fields = { ...event.detail.fields, AccountId: this.accountId, Origin: 'Web' };
if (fields.Priority === 'High') fields.Status = 'Escalated'; // business rule before saving
this.template.querySelector('lightning-record-edit-form').submit(fields);
}
handleSuccess(event) {
this.dispatchEvent(new ShowToastEvent({ title: 'Case created', message: event.detail.id, variant: 'success' }));
}
handleError(event) {
console.error(event.detail.message); // lightning-messages shows the details
}
}
The pattern above – preventDefault() in onsubmit, adjust event.detail.fields, then form.submit(fields) – is how you set hidden values (the parent account, the origin) and apply rules before the record is saved. lightning-messages shows server errors such as validation rules.
Which form?
| Component | Choose it when |
|---|---|
lightning-record-form | a standard view/edit/create form with a fixed set of fields is enough |
lightning-record-edit-form | you need a custom layout, default or hidden values, or logic before saving |
lightning-record-view-form | you want a custom read-only layout with lightning-output-field |
your own inputs + updateRecord/createRecord | the UI isn't field-by-field (a wizard, a slider, a calculated value) – next lesson |
Free security and consistency
All three forms respect field-level security, required fields, field types, picklist values and dependent picklists from the org's configuration. When an admin changes a picklist, your form updates without a deployment.
Practice
Try it yourself
Write a create form for Contact with First Name, Last Name and Email, using lightning-record-form in edit mode.
Show solution
<!-- newContact.html --> <template> <lightning-record-form object-api-name="Contact" fields={fields} mode="edit" onsuccess={handleSuccess}></lightning-record-form> </template>// newContact.js import { LightningElement } from 'lwc'; import FIRST from '@salesforce/schema/Contact.FirstName'; import LAST from '@salesforce/schema/Contact.LastName'; import EMAIL from '@salesforce/schema/Contact.Email'; export default class NewContact extends LightningElement { fields = [FIRST, LAST, EMAIL]; handleSuccess(event) { console.log('Created contact', event.detail.id); } }[record-form: Contact mode=edit fields: FirstName, LastName, Email]
Quick check
How do you make lightning-record-form a create form?
Leave out record-id.
How do you add a hidden value before an edit form saves?
preventDefault() in onsubmit, add the field to event.detail.fields, then call form.submit(fields).
Do record forms respect field-level security?
Yes – fields the user can't see or edit are handled automatically.
Summary
- lightning-record-form gives a complete view/edit/create form in a few lines.
- lightning-record-edit-form with lightning-input-field gives control over layout and submit logic.
- All record forms use LDS: security, validation and picklists come from the org.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.