Sub topic 5
View, edit and readonly modes
This chapter covers the mode attribute of lightning-record-form. It assumes you can build a basic lightning-record-form with object-api-name, record-id and fields or layout-type, and that you know how @api properties and event handlers work in a Lightning Web Component.
What the mode attribute controls
The mode attribute sets how the form behaves, not only how it looks. It decides whether users can edit, which buttons appear, and which state the form returns to after a save or cancel. It accepts three values: view, edit and readonly.
If you leave mode out, the component picks a default from the attributes you supplied:
- With a
record-id, the default isview. - Without a
record-id, the form creates a record and the default isedit.
The table summarizes the three modes.
| Mode | Fields shown | Pencil icons | Buttons | Typical use |
|---|---|---|---|---|
| `view` | Output (read) presentation | Yes, on fields the user can update | None until a pencil is clicked, then Save and Cancel | A record detail panel with inline editing |
| `edit` | Editable inputs | Not applicable | Save and Cancel (the form may also show Submit-style buttons depending on context) | Create a record, or update one directly |
| `readonly` | Output (read) presentation | No | None | Display only, no editing path |
Every mode still goes through Lightning Data Service, so sharing, object permissions and field-level security apply. A user who lacks read access to a field does not see it. A user who can read but not update a field sees it without a pencil in view mode, and it renders as read-only text in edit mode.
View mode
view is the default for an existing record. The form shows each field as formatted output. Fields the current user can update show a pencil icon on hover. Clicking any pencil switches the whole form into edit state, so every editable field becomes an input at once. The form then shows Save and Cancel buttons.
<template>
<lightning-record-form
object-api-name="Account"
record-id={recordId}
layout-type="Compact"
columns="2"
mode="view">
</lightning-record-form>
</template>After a successful save, or after Cancel, the form returns to view. This makes view a good fit for a detail card where editing is occasional.
Edit mode
In edit mode the form shows inputs straight away. What happens depends on whether you pass a record-id:
- No
record-id: the form creates a record. After a successful save,onsuccessfires with the new record ID inevent.detail.id. - With a
record-id: the form updates that record, and the inputs are visible from the start with no pencil step.
Here is a create form that uses fields imported from @salesforce/schema:
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
export default class AccountCreate extends LightningElement {
fields = [NAME_FIELD, INDUSTRY_FIELD];
handleSuccess(event) {
this.dispatchEvent(
new ShowToastEvent({
title: 'Account created',
message: 'Record ID: ' + event.detail.id,
variant: 'success'
})
);
}
}<template>
<lightning-record-form
object-api-name="Account"
fields={fields}
mode="edit"
onsuccess={handleSuccess}>
</lightning-record-form>
</template>Because this is an edit form without a record-id, the component creates the record. The mode="edit" attribute is redundant here, but stating it makes your intent clear to the next reader.
Readonly mode
readonly shows the record as output only. No pencil icons appear and no buttons render, so the user has no way to start an edit from the form. The component never makes a save call, even though the user may have edit permission on the record.
Use readonly when the page should only display data. This could be a summary beside an approval step, or a record the user is allowed to edit elsewhere but should not edit here. Controlling the mode is a UI decision, not a security control. Users with access can still change the record through another component, the standard page or the API. To prevent a change, use permissions, validation rules or sharing.
Moving between modes
The form changes state in two ways.
- User action: in
viewmode, clicking a pencil enters edit state. Save or Cancel returns to view. - Your code: you bind
modeto a property and change that property.
The second way lets you build a "Edit" button of your own, for example one that is shown only to certain users:
import { LightningElement, api } from 'lwc';
export default class AccountDetail extends LightningElement {
@api recordId;
mode = 'readonly';
get isReadonly() {
return this.mode === 'readonly';
}
handleEdit() {
this.mode = 'edit';
}
handleDone() {
this.mode = 'readonly';
}
}<template>
<template lwc:if={isReadonly}>
<lightning-button label="Edit" onclick={handleEdit}></lightning-button>
</template>
<lightning-record-form
object-api-name="Account"
record-id={recordId}
layout-type="Full"
mode={mode}
onsuccess={handleDone}
oncancel={handleDone}>
</lightning-record-form>
</template>Here the form starts as readonly. Clicking Edit sets mode to edit, which shows inputs and the form buttons. A successful save or a Cancel click returns it to readonly.
The diagram shows that readonly never changes by itself. Only your code moves the form out of it.
What mode does not change
- Field selection.
fieldsandlayout-typebehave the same in every mode. A field that is absent from the form inviewmode is absent ineditmode too. - Field-level security. A field without update access is never an input, whatever
modesays. - Validation. Required fields, validation rules and duplicate rules run on save in the same way. Failed saves show the error inside the form and fire
onerror. - Record type. A form for a record that has a record type shows the picklist values allowed for that record type. Mode does not affect this.
Common errors
- Setting
mode="edit"and expecting a pencil. Pencils exist only inviewmode. If users complain that they cannot click to edit, check that the mode is notreadonlyoredit. - Using
readonlyand expecting a Save button. There is none. Switch toedit, or useview. - Forgetting
record-idin a form meant to update. Without it the form creates a new record on save, which can produce duplicates. IfrecordIdarrives asynchronously, render the form only when it is available, for example withlwc:if={recordId}. - Expecting the form to leave
editafter saving. A form that you explicitly set toeditstays inedit. Handleonsuccessand change your boundmodeproperty if you want to switch back. - Hard-coding
modestrings with typos. An invalid value such as"editable"does not behave as any of the three modes. Keep the values in one place.
Choosing a mode
- Use
viewfor detail panels where editing is occasional and one click should be enough. - Use
editfor creation, or for pages whose main purpose is editing. - Use
readonlyfor display-only forms and for forms you switch toeditunder your own control.
If you find yourself adding many mode toggles, custom buttons and field-level tweaks, you are probably outgrowing lightning-record-form. The next chapters on lightning-record-edit-form give you control over layout and submission.
Exercise
A form with record-id, layout-type="Full" and no mode attribute shows a pencil icon next to some fields but not others. What mode is the form in, and why do some fields lack the pencil?
Show answer
The form is in view mode, the default when a record-id is supplied. A field lacks the pencil when the current user cannot update it, for example because field-level security makes it read-only or because the field is a system or formula field. Fields the user can update show the pencil, and clicking one puts the whole form into edit state with Save and Cancel buttons.
Exercise
You want a record summary that starts as display-only and becomes editable only after the user clicks your own Edit button. Which mode value do you bind to, which values does it switch between, and which events return it to the starting state?
Show answer
Bind mode={mode} to a component property that starts as readonly. The Edit button sets it to edit. Handle onsuccess and oncancel on the form and set mode back to readonly in those handlers.