getUpdatedFormFields
getUpdatedFormFields computes the difference between the initial form values and the current values, returning only the changed fields for a minimal PATCH payload.
Import
import { getUpdatedFormFields } from "@phpcreation/frontend-crud-react-nextjs-bundle/utils/getUpdatedFormFields";Usage
import { getUpdatedFormFields } from "...";
const onSubmit = async (currentValues) => {
const diff = getUpdatedFormFields(initialValues, currentValues);
if (Object.keys(diff).length === 0) {
toast("No changes to save.");
return;
}
await apiPatch(`/entities/${id}`, diff);
};Parameters
| Param | Type | Description |
|---|---|---|
initial | Record<string, unknown> | Values at form load time |
current | Record<string, unknown> | Current form values |
Returns
An object containing only the fields that have changed values.
Edge Cases
- Array fields (many-to-many): compared by value equality, not reference
- Empty string vs null: treated as equivalent (no change reported)
undefinedfields: excluded from the diff
Related
Last updated on