Skip to content

Commit f346aa5

Browse files
committed
AAE-28918 Fix start process button is enabled when required widgets are read only without value
1 parent 7b29d56 commit f346aa5

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ describe('FormFieldModel', () => {
12541254
expect(field.validate()).toBe(false);
12551255
});
12561256

1257-
it('should NOT validate readOnly field if it is NOT validatable', () => {
1257+
it('should validate readOnly required field even if type is not in validatable list', () => {
12581258
const form = new FormModel();
12591259
const field = new FormFieldModel(form, {
12601260
id: 'mockTextFieldId',
@@ -1267,8 +1267,60 @@ describe('FormFieldModel', () => {
12671267
const validator = new RequiredFieldValidator();
12681268
form.fieldValidators = [validator];
12691269

1270+
expect(FormFieldTypes.isValidatableType(FormFieldTypes.TEXT)).toBeFalse();
1271+
expect(field.validate()).toBe(false);
1272+
});
1273+
1274+
it('should pass validation for readOnly required field that has a value', () => {
1275+
const form = new FormModel();
1276+
const field = new FormFieldModel(form, {
1277+
id: 'mockTextFieldId',
1278+
type: FormFieldTypes.TEXT,
1279+
readOnly: true,
1280+
required: true,
1281+
value: 'some value'
1282+
});
1283+
1284+
const validator = new RequiredFieldValidator();
1285+
form.fieldValidators = [validator];
1286+
1287+
expect(field.validate()).toBe(true);
1288+
});
1289+
1290+
it('should skip validation for readOnly non-required field', () => {
1291+
const form = new FormModel();
1292+
const field = new FormFieldModel(form, {
1293+
id: 'mockTextFieldId',
1294+
type: FormFieldTypes.TEXT,
1295+
readOnly: true,
1296+
required: false,
1297+
value: null
1298+
});
1299+
1300+
const validator = new RequiredFieldValidator();
1301+
form.fieldValidators = [validator];
1302+
1303+
expect(field.validate()).toBe(true);
1304+
});
1305+
1306+
it('should pass validation for readOnly required field with empty value when field or parent is hidden', () => {
1307+
const form = new FormModel();
1308+
const field = new FormFieldModel(form, {
1309+
id: 'mockTextFieldId',
1310+
type: FormFieldTypes.TEXT,
1311+
readOnly: true,
1312+
required: true,
1313+
value: null
1314+
});
1315+
1316+
const validator = new RequiredFieldValidator();
1317+
form.fieldValidators = [validator];
1318+
1319+
spyOn(form, 'isFieldOrParentHidden').and.returnValue(true);
1320+
12701321
expect(FormFieldTypes.isValidatableType(FormFieldTypes.TEXT)).toBeFalse();
12711322
expect(field.validate()).toBe(true);
1323+
expect(form.isFieldOrParentHidden).toHaveBeenCalledWith(field);
12721324
});
12731325

12741326
it('should set the tooltip correctly', () => {

lib/core/src/lib/form/components/widgets/core/form-field.model.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ export class FormFieldModel extends FormWidgetModel {
178178
return this._isValid;
179179
}
180180
}
181+
} else if (this.readOnly && this._required && !this.form?.isFieldOrParentHidden(this) && this.isValueEmpty()) {
182+
this._isValid = false;
183+
return this._isValid;
181184
}
182185
this._isValid = true;
183186
return this._isValid;
@@ -187,6 +190,22 @@ export class FormFieldModel extends FormWidgetModel {
187190
return !this.readOnly || FormFieldTypes.isValidatableType(this.type);
188191
}
189192

193+
private isValueEmpty(): boolean {
194+
if (this.value === null || this.value === undefined || this.value === '') {
195+
return true;
196+
}
197+
if (typeof this.value === 'string' && this.value.trim().length === 0) {
198+
return true;
199+
}
200+
if (this.type === FormFieldTypes.BOOLEAN) {
201+
return !this.value;
202+
}
203+
if (Array.isArray(this.value)) {
204+
return this.value.length === 0;
205+
}
206+
return false;
207+
}
208+
190209
constructor(form: any, json?: any, parent?: RepeatableSectionModel) {
191210
super(form, json);
192211
if (json) {

0 commit comments

Comments
 (0)