Skip to Content
FrontendBundlesCRUDUtilitiesgetUpdatedFormFields

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

ParamTypeDescription
initialRecord<string, unknown>Values at form load time
currentRecord<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)
  • undefined fields: excluded from the diff

Last updated on