Sub topic 5
View, edit and readonly modes
This chapter assumes you can build a basic lightning-record-form with object-api-name, record-id and a field list. It also assumes you know how @api properties and event handlers work in a Lightning Web Component (LWC). Here you will learn how the mode attribute changes what the form shows and which mode to pick.
The three modes
The mode attribute on lightning-record-form accepts three values.
| Mode | Fields shown as | Buttons shown | Typical use |
|---|---|---|---|
| `view` | Read-only output, with a pencil icon on editable fields | None until the user clicks a pencil icon, then Save and Cancel | Detail page that allows occasional edits |
| `edit` | Input fields | Save and Cancel | Edit screen or create form |
| `readonly` | Read-only output, with no pencil icons | None | Display only |
If you set record-id and leave out mode, the form uses view. If you leave out record-id, the form creates a record and uses edit. In a create form, view is not meaningful because there is nothing to display yet.
View mode
In view mode, each field the user can edit shows a pencil icon. Clicking one switches the whole form into edit mode and reveals Save and Cancel buttons. After a successful save, or after Cancel, the form returns to view mode.
<template>
<lightning-record-form
object-api-name="Account"
record-id={recordId}
layout-type="Compact"
columns="2"
mode="view">
</lightning-record-form>
</template>A field with no pencil icon is not editable for this user. The cause can be field-level security, a formula field, or a system field such as CreatedDate.
Edit mode
Edit mode shows input fields immediately, along with Save and Cancel. Use it when the user opened the component specifically to change the record.
<template>
<lightning-record-form
object-api-name="Account"
record-id={recordId}
fields={fields}
mode="edit"
onsuccess={handleSuccess}>
</lightning-record-form>
</template>import { LightningElement, api } from 'lwc';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
export default class AccountEdit extends LightningElement {
@api recordId;
fields = [NAME_FIELD, INDUSTRY_FIELD];
handleSuccess() {
// The form saved the record. Add follow-up logic here.
}
}In edit mode, Cancel does not leave edit mode, because the form was never in view mode. It resets the inputs to the last saved values and fires the cancel event. Handle oncancel if you need to close a modal or navigate away.
Readonly mode
Readonly mode renders the fields as output and gives the user no way to edit. It is the right choice when the component is only a display, or when editing happens somewhere else.
<template>
<lightning-record-form
object-api-name="Account"
record-id={recordId}
layout-type="Compact"
mode="readonly">
</lightning-record-form>
</template>Readonly mode requires a record-id. It also hides the buttons, so the component fires no save or cancel events.
How the mode changes at runtime
The following diagram shows the transitions in a form that starts in view mode.
If a save fails, the form stays in the editing state so the user can correct the values. The error appears in the form and fires the error event.
Common mistakes
- Setting
mode="edit"and expecting a view first. Edit mode always opens with inputs. Useviewif you want the user to opt in to editing. - Using
readonlyand expecting pencil icons. Onlyviewhas them. - Passing
modeon a create form. Withoutrecord-idthe form is in edit mode, so settingmode="readonly"shows an empty display with nothing useful in it. - Assuming a missing pencil means a bug. Check field-level security and whether the field is a formula or system field first.
Choosing a mode
- Use
viewwhen most visits are for reading and editing is occasional. - Use
editwhen the user's purpose is to change the record, or when you are creating one. - Use
readonlywhen the component must never change data.
Exercise
A component shows an Account's details on a record page. Users should read the data by default, but be able to edit a field after clicking it. Which mode do you set, and what happens to the form after a successful save?
Show answer
Set mode="view", which is also the default when record-id is provided. Clicking a pencil icon switches the form to editing. After a successful save, the form fires the success event and returns to view mode.
Exercise
You set mode="edit" on a form, and a user clicks Cancel. Does the form switch to view mode? What does it do instead?
Show answer
No. An edit-mode form never was in view mode, so Cancel resets the inputs to the last saved values and fires the cancel event. Handle oncancel to close a dialog or navigate away.