Skip to content

Commit

Permalink
fix(core): take account of previously disabled control (#2910)
Browse files Browse the repository at this point in the history
fix #2907
  • Loading branch information
aitboudad committed Jul 5, 2021
1 parent f767102 commit 65c8a95
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
29 changes: 16 additions & 13 deletions src/core/src/lib/extensions/field-form/field-form.ts
Expand Up @@ -69,24 +69,27 @@ export class FieldFormExtension implements FormlyExtension {
if (field.key || !field.parent || (!field.key && !field.fieldGroup)) {
const { formControl: c } = field;
const disabled = field.templateOptions ? field.templateOptions.disabled : false;
if (disabled && c.enabled) {
c.disable({ emitEvent: false, onlySelf: true });
if (!c.parent) {
updateControlValidity(c);
} else {
updateValidity = true;
if (field.key) {
if (disabled && c.enabled) {
c.disable({ emitEvent: false, onlySelf: true });
if (!c.parent) {
updateControlValidity(c);
} else {
updateValidity = true;
}
}
}

if (!disabled && c.disabled) {
c.enable({ emitEvent: false, onlySelf: true });
if (!c.parent) {
updateControlValidity(c);
} else {
updateValidity = true;
if (!disabled && c.disabled) {
c.enable({ emitEvent: false, onlySelf: true });
if (!c.parent) {
updateControlValidity(c);
} else {
updateValidity = true;
}
}
}


if (null === c.validator || null === c.asyncValidator) {
c.setValidators(() => {
const v = Validators.compose(this.mergeValidators<ValidatorFn>(field, '_validators'));
Expand Down
32 changes: 24 additions & 8 deletions src/core/src/lib/services/formly.form.builder.spec.ts
Expand Up @@ -245,18 +245,34 @@ describe('FormlyFormBuilder service', () => {
});

it('should enable previously disabled control', () => {
const field: FormlyFieldConfig = {
key: 'address',
templateOptions: { disabled: true },
};
let fields: FormlyFieldConfig[] = [
{
key: 'foo',
templateOptions: { disabled: true },
},
{
key: 'bar',
templateOptions: { disabled: true },
},
];

builder.buildForm(form, [field], {}, {});
builder.buildForm(form, fields, {}, {});

const control = field.formControl;
expect(control.disabled).toEqual(true);
expect(form.get('foo').disabled).toEqual(true);
expect(form.get('bar').disabled).toEqual(true);

fields = [
{
key: 'foo',
templateOptions: { disabled: true },
},
{ key: 'bar' },
];

builder.buildForm(form, [field], {}, {});
expect(control.disabled).toEqual(false);
builder.buildForm(form, fields, {}, {});
expect(form.get('foo').disabled).toEqual(true);
expect(form.get('bar').disabled).toEqual(false);
});

describe('assign model to fields', () => {
Expand Down

0 comments on commit 65c8a95

Please sign in to comment.