Skip to content

Commit

Permalink
Form data are completely isolated from their source #9844
Browse files Browse the repository at this point in the history
Because we will submit form data for mutation, and because we merge the
mutation result back into the form data via
`NaturalAbstractModelService.updateNow()`, then we must make triple-sure
 that the form is not populated with readonly objects coming from our
 API. Otherwise, a structure like the following would raise JS errors
 after mutation:

 ```json
 {
    id: 123,
    name: 'foo',
    product: {
        id: 456,
        quantity: 5, // This used to fail if fetched from mutation
    }
}
 ```
  • Loading branch information
PowerKiKi committed Mar 22, 2024
1 parent 145cf71 commit 8bd6822
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion projects/natural/src/lib/services/abstract-model.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {makePlural, mergeOverrideArray, relationsToIds, upperCaseFirstLetter} fr
import {PaginatedData} from '../classes/data-source';
import {NaturalDebounceService} from './debounce.service';
import {ApolloQueryResult} from '@apollo/client/core/types';
import {deepClone} from '../modules/search/classes/utils';

export type FormValidators = Record<string, ValidatorFn[]>;

Expand Down Expand Up @@ -143,7 +144,7 @@ export abstract class NaturalAbstractModelService<
* `getFormGroupValidators`, `getFormGroupAsyncValidators` might be.
*/
public getFormGroup(model: Literal): UntypedFormGroup {
const formConfig = this.getFormConfig(model);
const formConfig = this.getFormConfig(deepClone(model));
return new UntypedFormGroup(formConfig, {
validators: this.getFormGroupValidators(model),
asyncValidators: this.getFormGroupAsyncValidators(model),
Expand Down

0 comments on commit 8bd6822

Please sign in to comment.