# When should you use lightning-record-form versus lightning-record-edit-form for data entry?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Record Form, Lightning Data Service

Read it on the site: https://www.forcechallenges.com/interview-questions/when-should-you-use-lightning-record-form-versus-lightning-record-edit-form-for-data-entry

## Answer

**Basics:** Both are Lightning Data Service components, so they need no Apex, enforce field-level security, and respect record types and validation rules. The difference is flexibility. `record-form` generates the whole form from field lists or the page layout. `record-edit-form` is a container where you place each field yourself.

**How it works:**
1. `lightning-record-form`: set `object-api-name`, optionally `record-id`, then either `fields={fields}` or `layout-type="Compact|Full"`, and `mode` (`view`, `edit`, `readonly`). It renders its own Save and Cancel buttons and fires `onsuccess`, `onerror`, `onsubmit`, `oncancel`.
2. `lightning-record-edit-form`: set `object-api-name` and `record-id` (omit for create). Add `lightning-input-field field-name="..."` elements, `lightning-messages` for errors, and your own submit button (`type="submit"`).
3. Intercept `onsubmit`: `event.preventDefault()` then modify `event.detail.fields` and call `this.template.querySelector('lightning-record-edit-form').submit(fields)`.
4. `onsuccess` returns `event.detail.id`.

**Advanced / deep dive:** `record-form` is limited: you cannot reorder or insert custom elements between fields, and it has restricted customization of buttons. `record-edit-form` allows conditional fields (`lwc:if`), default values, field-by-field styling, multi-column layouts, and custom buttons. `lightning-record-view-form` is the read-only counterpart. Neither supports saving multiple records or related-object children in one transaction; use Apex for that. They also do not support every field type in every context, so test compound and special fields. Pass `record-type-id` to get the correct picklist values for a record type. Both share LDS cache, so other components on the page update after save. Use `lightning-input-field` only inside an edit form; elsewhere use `lightning-input`.

**Code / config example:**
```html
<lightning-record-edit-form object-api-name="Case" onsubmit={handleSubmit} onsuccess={handleSuccess}>
    <lightning-messages></lightning-messages>
    <lightning-input-field field-name="Subject"></lightning-input-field>
    <lightning-input-field field-name="Priority"></lightning-input-field>
    <lightning-button type="submit" variant="brand" label="Create"></lightning-button>
</lightning-record-edit-form>
```
```javascript
handleSubmit(event) {
    event.preventDefault();
    const fields = { ...event.detail.fields, Origin: 'Web' };   // add or override a value
    this.template.querySelector('lightning-record-edit-form').submit(fields);
}
```

**Hidden gotchas & must-remember points:**
- `lightning-input-field` works only inside `lightning-record-edit-form`.
- `layout-type` and `fields` are alternatives in `record-form`; supply one, not both.
- Without `lightning-messages`, server-side errors may not display.
- Call `event.preventDefault()` before `submit(fields)` or you get a double submission.
- FLS applies: fields the user cannot access are not rendered.
- No Apex means no multi-object transactions or complex business logic.

**Best practice:** Start with `record-form`; move to `record-edit-form` as soon as you need custom layout, defaults or submit-time logic.

---
Source: ForceChallenges · last updated 2026-11-02
