diff --git a/lib/core/form/components/widgets/core/form-field.model.spec.ts b/lib/core/form/components/widgets/core/form-field.model.spec.ts index ff5f0fc0f3..afea033f31 100644 --- a/lib/core/form/components/widgets/core/form-field.model.spec.ts +++ b/lib/core/form/components/widgets/core/form-field.model.spec.ts @@ -249,6 +249,150 @@ describe('FormFieldModel', () => { expect(field.value).toBe('28-04-2017'); }); + it('should parse the checkbox set to "true" when it is readonly', () => { + const form = new FormModel(); + const field = new FormFieldModel(form, { + fieldType: 'FormFieldRepresentation', + id: 'checkbox', + name: 'Checkbox', + type: 'readonly', + value: 'true', + required: false, + readOnly: true, + params: { + field: { + id: 'checkbox', + name: 'Checkbox', + type: 'boolean', + value: null, + required: false, + readOnly: false + } + } + }); + expect(field.value).toBeTruthy(); + }); + + it('should parse the checkbox set to null when it is readonly', () => { + const form = new FormModel(); + const field = new FormFieldModel(form, { + fieldType: 'FormFieldRepresentation', + id: 'checkbox', + name: 'Checkbox', + type: 'readonly', + value: null, + required: false, + readOnly: true, + params: { + field: { + id: 'checkbox', + name: 'Checkbox', + type: 'boolean', + value: null, + required: false, + readOnly: false + } + } + }); + expect(field.value).toBeFalsy(); + }); + + it('should parse the checkbox set to "false" when it is readonly', () => { + const form = new FormModel(); + const field = new FormFieldModel(form, { + fieldType: 'FormFieldRepresentation', + id: 'checkbox', + name: 'Checkbox', + type: 'readonly', + value: 'false', + required: false, + readOnly: true, + params: { + field: { + id: 'checkbox', + name: 'Checkbox', + type: 'boolean', + value: null, + required: false, + readOnly: false + } + } + }); + expect(field.value).toBeFalsy(); + }); + + it('should parse the checkbox set to "true" when it is editable', () => { + const form = new FormModel(); + const field = new FormFieldModel(form, { + fieldType: 'FormFieldRepresentation', + id: 'checkbox', + name: 'Checkbox', + type: 'boolean', + value: 'true', + required: false, + readOnly: true, + params: { + field: { + id: 'checkbox', + name: 'Checkbox', + type: 'boolean', + value: null, + required: false, + readOnly: false + } + } + }); + expect(field.value).toBeTruthy(); + }); + + it('should parse the checkbox set to null when it is editable', () => { + const form = new FormModel(); + const field = new FormFieldModel(form, { + fieldType: 'FormFieldRepresentation', + id: 'checkbox', + name: 'Checkbox', + type: 'boolean', + value: null, + required: false, + readOnly: true, + params: { + field: { + id: 'checkbox', + name: 'Checkbox', + type: 'boolean', + value: null, + required: false, + readOnly: false + } + } + }); + expect(field.value).toBeFalsy(); + }); + + it('should parse the checkbox set to "false" when it is editable', () => { + const form = new FormModel(); + const field = new FormFieldModel(form, { + fieldType: 'FormFieldRepresentation', + id: 'checkbox', + name: 'Checkbox', + type: 'boolean', + value: 'false', + required: false, + readOnly: true, + params: { + field: { + id: 'checkbox', + name: 'Checkbox', + type: 'boolean', + value: null, + required: false, + readOnly: false + } + } + }); + expect(field.value).toBeFalsy(); + }); + it('should return the label of selected dropdown value ', () => { const field = new FormFieldModel(new FormModel(), { type: FormFieldTypes.DROPDOWN, diff --git a/lib/core/form/components/widgets/core/form-field.model.ts b/lib/core/form/components/widgets/core/form-field.model.ts index 9290e04800..83d21eaf74 100644 --- a/lib/core/form/components/widgets/core/form-field.model.ts +++ b/lib/core/form/components/widgets/core/form-field.model.ts @@ -320,7 +320,7 @@ export class FormFieldModel extends FormWidgetModel { } } - if (json.type === FormFieldTypes.BOOLEAN) { + if (this.isCheckboxField(json)) { value = json.value === 'true' || json.value === true ? true : false; } @@ -447,4 +447,11 @@ export class FormFieldModel extends FormWidgetModel { json.type === FormFieldTypes.DATETIME; } + private isCheckboxField(json: any): boolean { + return (json.params && + json.params.field && + json.params.field.type === FormFieldTypes.BOOLEAN) || + json.type === FormFieldTypes.BOOLEAN; + } + }