diff --git a/lib/core/src/lib/form/components/widgets/core/form-field-validator.spec.ts b/lib/core/src/lib/form/components/widgets/core/form-field-validator.spec.ts index ac9756de2b..015b2854d0 100644 --- a/lib/core/src/lib/form/components/widgets/core/form-field-validator.spec.ts +++ b/lib/core/src/lib/form/components/widgets/core/form-field-validator.spec.ts @@ -120,6 +120,26 @@ describe('FormFieldValidator', () => { expect(validator.validate(field)).toBe(false); }); + it('should fail (display error) for text with only whitespace', () => { + const field = new FormFieldModel(new FormModel(), { + type: FormFieldTypes.TEXT, + value: ' ', + required: true + }); + + expect(validator.validate(field)).toBe(false); + }); + + it('should fail (display error) for multiline text with only whitespace', () => { + const field = new FormFieldModel(new FormModel(), { + type: FormFieldTypes.MULTILINE_TEXT, + value: ' \t\n ', + required: true + }); + + expect(validator.validate(field)).toBe(false); + }); + it('should succeed for date', () => { const field = new FormFieldModel(new FormModel(), { type: FormFieldTypes.DATE, diff --git a/lib/core/src/lib/form/components/widgets/core/form-field-validator.ts b/lib/core/src/lib/form/components/widgets/core/form-field-validator.ts index 0aa6680960..e6730630e3 100644 --- a/lib/core/src/lib/form/components/widgets/core/form-field-validator.ts +++ b/lib/core/src/lib/form/components/widgets/core/form-field-validator.ts @@ -72,6 +72,10 @@ export class RequiredFieldValidator implements FormFieldValidator { if (field.value === null || field.value === undefined || field.value === '') { return false; } + + if (typeof field.value === 'string' && field.value.trim().length === 0) { + return false; + } } return true; } diff --git a/lib/core/src/lib/form/components/widgets/widget.component.spec.ts b/lib/core/src/lib/form/components/widgets/widget.component.spec.ts index c6b331e0d5..d894a7c349 100644 --- a/lib/core/src/lib/form/components/widgets/widget.component.spec.ts +++ b/lib/core/src/lib/form/components/widgets/widget.component.spec.ts @@ -116,4 +116,40 @@ describe('WidgetComponent', () => { widget.field = new FormFieldModel(null, { required: true }); expect(widget.isRequired()).toBeTruthy(); }); + + describe('isInvalidFieldRequired', () => { + it('should return true when required field has no value', () => { + widget.field = new FormFieldModel(new FormModel(), { + type: 'text', + required: true, + value: null + }); + widget.field.markAsInvalid(); + widget.field.validationSummary = null; + + expect(widget.isInvalidFieldRequired()).toBe(true); + }); + + it('should return true when required field has whitespace-only value', () => { + widget.field = new FormFieldModel(new FormModel(), { + type: 'text', + required: true, + value: ' ' + }); + widget.field.markAsInvalid(); + + expect(widget.isInvalidFieldRequired()).toBe(true); + }); + + it('should return false when required field has a non-whitespace value', () => { + widget.field = new FormFieldModel(new FormModel(), { + type: 'text', + required: true, + value: 'hello' + }); + widget.field.markAsValid(); + + expect(widget.isInvalidFieldRequired()).toBe(false); + }); + }); }); diff --git a/lib/core/src/lib/form/components/widgets/widget.component.ts b/lib/core/src/lib/form/components/widgets/widget.component.ts index a328774bae..f5e16d4b50 100644 --- a/lib/core/src/lib/form/components/widgets/widget.component.ts +++ b/lib/core/src/lib/form/components/widgets/widget.component.ts @@ -88,7 +88,8 @@ export class WidgetComponent implements AfterViewInit { } isInvalidFieldRequired() { - return !this.field.isValid && (!this.field.validationSummary || !this.field.value) && this.isRequired(); + const isValueEmpty = !this.field.value || (typeof this.field.value === 'string' && this.field.value.trim().length === 0); + return !this.field.isValid && (!this.field.validationSummary || isValueEmpty) && this.isRequired(); } ngAfterViewInit() {