Sub topic 8
Using lightning-input-field, lightning-output-field and lightning-messages
This chapter assumes you can write a basic Lightning Web Component and know what a lightning-record-edit-form is from the previous chapter. It covers the three child components that you place inside that form.
The three child components
lightning-record-edit-form supplies the record context and the save behavior. It does not render any fields itself. You add them with these components:
| Component | Purpose | Where it can be used |
|---|---|---|
| `lightning-input-field` | Editable input for one field | Inside `lightning-record-edit-form` |
| `lightning-output-field` | Read-only label and value for one field | Inside `lightning-record-edit-form` or `lightning-record-view-form` |
| `lightning-messages` | Shows save errors and other form-level messages | Inside `lightning-record-edit-form` |
All three components get their field metadata from Lightning Data Service, so you don't write any Apex.
lightning-input-field
Set field-name to the API name of the field. The component then picks the right control for the field's data type. A date field gets a date picker. A picklist gets a dropdown with the values that are valid for the record type. A lookup gets a record search box.
The component also applies the field's label, help text and required setting. If the user lacks field-level security access to a field, the component doesn't render it.
Each input field is submitted with the form when the user clicks a button with type="submit". You don't need to read values yourself.
Useful attributes:
field-name: required. The field's API name.value: sets an initial value, for example on a create form.required: marks the field required in the UI. It does not change the field definition on the server.disabled: shows the field but blocks editing.variant: set tolabel-hiddenorlabel-inlineto change the label position.
lightning-output-field
Use lightning-output-field when the user should see a value but not change it. It formats the value by type, so currency, dates and links appear as they do on a standard page. It needs a record-id on the parent form, because it has nothing to display without one.
lightning-messages
Put one lightning-messages element in the form. When a save fails, it displays the error, such as a validation rule message or a required-field error. If you leave it out, the user sees field-level errors on the inputs but may miss errors that apply to the whole record.
Putting them together
<template>
<lightning-record-edit-form
object-api-name="Account"
record-id={recordId}>
<lightning-messages></lightning-messages>
<lightning-output-field field-name="Name"></lightning-output-field>
<lightning-input-field field-name="Industry"></lightning-input-field>
<lightning-input-field field-name="Phone"></lightning-input-field>
<lightning-button
class="slds-m-top_small"
type="submit"
label="Save">
</lightning-button>
</lightning-record-edit-form>
</template>The component class only needs to receive the record ID.
import { LightningElement, api } from 'lwc';
export default class AccountEditor extends LightningElement {
@api recordId;
}In this form, Name is read-only, and Industry and Phone are saved when the user clicks Save.
Common errors
- A field doesn't appear. The user may lack field-level security access, or the API name is misspelled. Custom fields end in
__c. - An output field is blank on a create form. Without a
record-id, there is no record to read from. - A save fails with no visible message. Add
lightning-messages. - Using
lightning-input-fieldoutside a form. It has no record context there, so it won't work.
Exercise
Your form shows a record's Name, but users must not edit it. They should edit Phone, and they must see an error message if a validation rule rejects the save. Which components do you use for each requirement?
Show answer
Use lightning-output-field with field-name="Name" for the read-only value, lightning-input-field with field-name="Phone" for the editable value, and lightning-messages for the validation rule error. All three go inside lightning-record-edit-form, which also needs a submit button.