Skip to content

Commit 6dbe4b7

Browse files
committed
fix(forms): memoize deepSignal value
Test that submission errors are cleared from their corresponding field when its value is changed. Submission errors should not be cleared from fields whose values remain unchanged. This behavior relies on fields memoizing their value, which `deepSignal()` now does.
1 parent cd8e0c3 commit 6dbe4b7

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

packages/forms/experimental/src/util/deep_signal.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {Signal, untracked, WritableSignal} from '@angular/core';
9+
import {computed, Signal, untracked, WritableSignal} from '@angular/core';
1010
import {SIGNAL} from '@angular/core/primitives/signals';
1111
import {isArray} from './type_guards';
1212

@@ -22,9 +22,8 @@ export function deepSignal<S, K extends keyof S>(
2222
source: WritableSignal<S>,
2323
prop: Signal<K>,
2424
): WritableSignal<S[K]> {
25-
const read: WritableSignal<S[K]> = (() => {
26-
return source()[prop()];
27-
}) as WritableSignal<S[K]>;
25+
// Memoize the property.
26+
const read = computed(() => source()[prop()]) as WritableSignal<S[K]>;
2827

2928
read[SIGNAL] = source[SIGNAL];
3029
read.set = (value: S[K]) => {

packages/forms/experimental/test/node/submit.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@ describe('submit', () => {
199199
await expectAsync(submitPromise).toBeRejectedWith(error);
200200
expect(f().submitting()).toBe(false);
201201
});
202+
203+
it('errors are cleared on edit', async () => {
204+
const data = signal({first: '', last: ''});
205+
const f = form(data, {injector: TestBed.inject(Injector)});
206+
207+
await submit(f, async (form) => {
208+
return [
209+
ValidationError.custom({kind: 'submit', field: f.first}),
210+
ValidationError.custom({kind: 'submit', field: f.last}),
211+
];
212+
});
213+
214+
expect(f.first().errors()).toEqual([ValidationError.custom({kind: 'submit'})]);
215+
expect(f.last().errors()).toEqual([ValidationError.custom({kind: 'submit'})]);
216+
217+
f.first().value.set('Hello');
218+
219+
expect(f.first().errors()).toEqual([]);
220+
expect(f.last().errors()).toEqual([ValidationError.custom({kind: 'submit'})]);
221+
});
202222
});
203223

204224
/**

0 commit comments

Comments
 (0)