# How does lightning-record-edit-form enable granular field-level customization during record creation and editing?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Record Edit Form, Custom Validation

Read it on the site: https://www.forcechallenges.com/interview-questions/how-does-lightning-record-edit-form-enable-granular-field-level-customization-during-record-creation-and

## Answer

**Basics:** Unlike `lightning-record-form` (auto layout) and `lightning-record-view-form` (read-only), the edit form lets you pick exactly which fields to show, in any markup. Set `object-api-name`, add `record-id` to edit (omit to create), place `lightning-input-field field-name="Name"` components and a submit button. Labels, required flags, picklist values and lookups come from metadata automatically.

**How it works:** Clicking a `lightning-button type="submit"` fires `onsubmit`. If you do nothing it saves the field values. To customize, call `event.preventDefault()`, read or modify `event.detail.fields`, then call `this.template.querySelector('lightning-record-edit-form').submit(fields)`. Other events: `onload`, `onsuccess` (`event.detail.id`), `onerror` (`event.detail`), and `lightning-messages` displays server and validation errors. `lightning-output-field` can be placed inside to display read-only values.

**Advanced / deep dive:** It uses Lightning Data Service and UI API, so it enforces object CRUD, field-level security and sharing, runs validation rules and triggers on the server, and updates the LDS cache so other components on the page refresh. Custom validation: server-side validation rules are the source of truth; client-side checks belong in `onsubmit` (cross-field rules, then `preventDefault` and show your own error) or via `reportValidity()` on fields. Defaults for create can be set with `value` on input fields. It handles one record; for related-record writes or transactional multi-object saves use Apex. Not all field types or compound fields behave the same in `lightning-input-field`; test addresses, rich text and polymorphic lookups. Dynamic forms in the App Builder are the declarative alternative for layout control without code.

**Code / config example:**
```html
<lightning-record-edit-form object-api-name="Opportunity" record-id={recordId}
        onsubmit={handleSubmit} onsuccess={handleSuccess}>
    <lightning-messages></lightning-messages>
    <lightning-input-field field-name="Name"></lightning-input-field>
    <lightning-input-field field-name="Amount"></lightning-input-field>
    <lightning-input-field field-name="StageName"></lightning-input-field>
    <lightning-button type="submit" label="Save" variant="brand"></lightning-button>
</lightning-record-edit-form>
```
```javascript
handleSubmit(event) {
    event.preventDefault();
    const fields = { ...event.detail.fields };
    if (fields.Amount < 0) { this.error = 'Amount cannot be negative'; return; }
    fields.Description = 'Edited via LWC';
    this.template.querySelector('lightning-record-edit-form').submit(fields);
}
```

**Hidden gotchas & must-remember points:**
- `lightning-input-field` must be a descendant of the form to work.
- Fields without FLS edit access render read-only or not at all.
- Required status comes from field metadata; client logic cannot make server rules optional.
- Forgetting `preventDefault()` means your modifications are ignored and the default save runs.
- Calling `submit()` with no argument submits current values; arguments override them.
- It does not use Apex, so Apex-only logic is not involved, but triggers still fire.

**Best practice:** Use the form for standard CRUD with custom layout, keep business rules in validation rules/triggers, and use `onsubmit` only for small, UI-level adjustments.

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