Skip to content

Commit a6174d5

Browse files
MeAkibmattrbeck
authored andcommitted
refactor(forms): warn when a text input receives a null value in Signal Forms
Native text `<input type="text">` controls do not support `null` values. When a Signal Forms model bound to a text input is set to `null`, the value is silently coerced to an empty string. (cherry picked from commit 37f3279)
1 parent c89f71a commit a6174d5

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

packages/forms/signals/src/directive/native.ts

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

9-
import {untracked} from '@angular/core';
9+
import {ɵformatRuntimeError as formatRuntimeError, untracked} from '@angular/core';
1010
import {NativeInputParseError, WithoutFieldTree} from '../api/rules';
1111
import type {ParseResult} from '../api/transformed_value';
12+
import {RuntimeErrorCode} from '../errors';
1213
import type {InputValidityMonitor} from './input_validity_monitor';
1314

1415
// Re-export shared native utilities from main forms package
1516
export {
16-
ɵisNativeFormElement as isNativeFormElement,
1717
ɵelementAcceptsMinMax as elementAcceptsMinMax,
18+
ɵisNativeFormElement as isNativeFormElement,
1819
ɵisTextualFormElement as isTextualFormElement,
1920
ɵsetNativeDomProperty as setNativeDomProperty,
2021
type ɵNativeFormControl as NativeFormControl,
@@ -145,6 +146,15 @@ export function setNativeControlValue(element: NativeFormControl, value: unknown
145146
return;
146147
}
147148
if (value === null) {
149+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
150+
console.warn(
151+
formatRuntimeError(
152+
RuntimeErrorCode.TEXT_INPUT_NULL_VALUE,
153+
`The text input ${element.name} received a null value. Text inputs should use empty strings to represent null values. ` +
154+
` The input's value will be set to an empty string instead.`,
155+
),
156+
);
157+
}
148158
element.value = '';
149159
return;
150160
}

packages/forms/signals/src/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ export const enum RuntimeErrorCode {
3030
MISSING_SUBMIT_ACTION = 1915,
3131
RENDERED_HIDDEN_FIELD = 1916,
3232
UNSUPPORTED_FEATURE = 1920,
33+
TEXT_INPUT_NULL_VALUE = 1921,
3334
}

0 commit comments

Comments
 (0)