Sub topic 12
Choosing the right form component
This chapter assumes you can build a basic LWC with @api properties and event handlers, and that you have seen lightning-record-form and lightning-record-edit-form in earlier chapters. Here you will learn how to pick between the base form components for a given requirement.
The three candidates
All three components use Lightning Data Service (LDS). They respect sharing, object permissions and field-level security, and they need no Apex.
lightning-record-formis the quickest option. One element gives you a view, edit or create form. You choose fields withfieldsorlayout-type, and you get default buttons and default layout.lightning-record-edit-formis the customizable editing option. You placelightning-input-fieldelements yourself, add your own buttons, and handleonsubmit,onsuccessandonerror.lightning-record-view-formis the customizable read-only option. You placelightning-output-fieldelements yourself. It has no save step.
Decision criteria
| Requirement | Best fit |
|---|---|
| Show or edit a record with the fewest lines of code | `lightning-record-form` |
| Use the same fields as the page layout | `lightning-record-form` with `layout-type` |
| Control field order, grouping or sections | `lightning-record-edit-form` or `lightning-record-view-form` |
| Add custom buttons or a custom save flow | `lightning-record-edit-form` |
| Change or validate values before saving | `lightning-record-edit-form` with an `onsubmit` handler |
| Show a record read-only with a custom layout | `lightning-record-view-form` |
| Mix editable and read-only fields | `lightning-record-edit-form` with `lightning-output-field` |
Same job, two implementations
Suppose you need a form that creates a Contact. With lightning-record-form, this is enough:
<template>
<lightning-record-form
object-api-name="Contact"
fields={fields}
onsuccess={handleSuccess}>
</lightning-record-form>
</template>Now suppose a requirement arrives: trim the last name before saving. lightning-record-form also fires onsubmit, but its layout and buttons stay fixed. Once you also need a custom button row or grouped sections, switch to lightning-record-edit-form:
handleSubmit(event) {
event.preventDefault();
const fields = event.detail.fields;
fields.LastName = (fields.LastName || '').trim();
this.template
.querySelector('lightning-record-edit-form')
.submit(fields);
}The onsubmit handler cancels the default save, changes the values, and then calls submit() with the modified fields.
Trade-offs
Convenience costs control. lightning-record-form is compact, but you cannot rearrange its buttons or place other elements between fields. With layout-type, the fields come from the page layout, so an admin's layout change alters your component without a code change. That can be a benefit or a surprise. If you need a stable field list, use fields instead.
lightning-record-edit-form takes more markup, but each field is explicit. It also reads record type and field metadata for you, so picklist values and required markers still follow the object's configuration.
Do not move to a custom form just because you might need one later. Start with lightning-record-form, and switch when a concrete requirement cannot be met.
Checklist
- Does the form need only a standard list of fields? Use
lightning-record-form. - Does it need a custom layout, custom buttons or pre-save changes? Use
lightning-record-edit-form. - Is it read-only with a custom layout? Use
lightning-record-view-form. - Does it span several objects or need server-side logic? Use Apex or
uiRecordApi.
Exercise
A product team wants a form that edits an Account's Name and Industry. The Save button must sit in a custom footer, and Name must be upper-cased before it is saved. Which component do you choose, and which event do you handle?
Show answer
Use lightning-record-edit-form, because it lets you place your own submit button and arrange the fields. Handle onsubmit: call event.preventDefault(), set fields.Name = fields.Name.toUpperCase() on event.detail.fields, then call submit(fields) on the form.
Exercise
An admin changes the page layout for Contact, and your lightning-record-form suddenly shows a new field. Why did this happen, and how do you prevent it?
Show answer
The component uses layout-type, so the fields come from the page layout. To keep a fixed set of fields, replace layout-type with a fields array of imported @salesforce/schema references.