8000 fix(forms): only touch visible, interactive fields on submit by leonsenft · Pull Request #66785 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/forms/signals/src/api/structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.dev/license
*/

import {inject, Injector, runInInjectionContext, WritableSignal} from '@angular/core';
import {inject, Injector, runInInjectionContext, untracked, WritableSignal} from '@angular/core';

import {BasicFieldAdapter, FieldAdapter} from '../field/field_adapter';
import {FormFieldManager} from '../field/manager';
Expand Down Expand Up @@ -369,10 +369,13 @@ export async function submit<TModel>(
action: (form: FieldTree<TModel>) => Promise<TreeValidationResult>,
) {
const node = form() as unknown as FieldNode;
markAllAsTouched(node);
const invalid = untracked(() => {
markAllAsTouched(node);
return node.invalid();
});

// Fail fast if the form is already invalid.
if (node.invalid()) {
if (invalid) {
return;
}

Expand Down Expand Up @@ -429,6 +432,12 @@ export function schema<TValue>(fn: SchemaFn<TValue>): Schema<TValue> {

/** Marks a {@link node} and its descendants as touched. */
function markAllAsTouched(node: FieldNode) {
// Don't mark hidden, disabled, or readonly fields as touched since they don't contribute to the
// form's validity. This also prevents errors from appearing immediately if they're later made
// interactive.
if (node.validationState.shouldSkipValidation()) {
return;
}
node.markAsTouched();
for (const child of node.structure.children()) {
markAllAsTouched(child);
Expand Down
78 changes: 78 additions & 0 deletions packages/forms/signals/test/node/submit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import {Injector, resource, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {
disabled,
form,
required,
requiredError,
submit,
validateAsync,
ValidationError,
hidden,
readonly,
} from '../../public_api';

describe('submit', () => {
Expand Down Expand Up @@ -269,6 +272,81 @@ describe('submit', () => {
expect(f.first().errors()).toEqual([]);
expect(f.last().errors()).toEqual([{kind: 'submit', fieldTree: f.last}]);
});

it('does not mark disabled fields as touched', async () => {
const data = signal({first: '', last: ''});
const f = form(
data,
(name) => {
// Disable first name when last name is empty.
disabled(name.first, ({valueOf}) => valueOf(name.last) === '');
},
{injector: TestBed.inject(Injector)},
);

expect(f.first().disabled()).toBe(true);
expect(f.first().touched()).toBe(false);
expect(f.last().touched()).toBe(false);

await submit(f, async () => []);
expect(f.first().touched()).toBe(false);
expect(f.last().touched()).toBe(true);

// Set last name to make first name enabled.
f.last().value.set('Doe');
expect(f.first().disabled()).toBe(false);
expect(f.first().touched()).toBe(false);
});

it('does not mark hidden fields as touched', async () => {
const data = signal({first: '', last: ''});
const f = form(
data,
(name) => {
// Hide first name when last name is empty.
hidden(name.first, ({valueOf}) => valueOf(name.last) === '');
},
{injector: TestBed.inject(Injector)},
);

expect(f.first().hidden()).toBe(true);
expect(f.first().touched()).toBe(false);
expect(f.last().touched()).toBe(false);

await submit(f, async () => []);
expect(f.first().touched()).toBe(false);
expect(f.last().touched()).toBe(true);

// Set last name to make first name visible.
f.last().value.set('Doe');
expect(f.first().hidden()).toBe(false);
expect(f.first().touched()).toBe(false);
});

it('does not mark readonly fields as touched', async () => {
const data = signal({first: '', last: ''});
const f = form(
data,
(name) => {
// Make first name readonly when last name is empty.
readonly(name.first, ({valueOf}) => valueOf(name.last) === '');
},
{injector: TestBed.inject(Injector)},
);

expect(f.first().readonly()).toBe(true);
expect(f.first().touched()).toBe(false);
expect(f.last().touched()).toBe(false);

await submit(f, async () => []);
expect(f.first().touched()).toBe(false);
expect(f.last().touched()).toBe(true);

// Set last name to make first name enabled.
f.last().value.set('Doe');
expect(f.first().readonly()).toBe(false);
expect(f.first().touched()).toBe(false);
});
});

/**
Expand Down
0