Skip to content

Commit

Permalink
fix(#514): avoid supress and/or force validation messages. (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
aitboudad authored Oct 9, 2017
1 parent bad3ecb commit 1ffe899
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 16 deletions.
2 changes: 1 addition & 1 deletion demo/src/formly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const NgFormlyConfig: ConfigOption = {
],
extras: {
showError: (field: Field) => {
return (field.formState.submitted || field.formControl.touched) && !field.formControl.valid;
return (field.formState.submitted || field.formControl.touched || (field.field.validation && field.field.validation.show)) && !field.formControl.valid;
},
},
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ngx-formly/platform",
"version": "2.0.0-alpha.2",
"version": "2.0.0-alpha.3",
"author": "Zama Khan Mohammed <mohammedzamakhan@gmail.com>",
"contributors": [
"Zama Khan Mohammed <mohammedzamakhan@gmail.com>",
Expand Down
6 changes: 0 additions & 6 deletions src/core/src/components/formly.field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,6 @@ export class FormlyField implements DoCheck, OnInit, OnDestroy {
if (formControl.status !== 'DISABLED' && this.field.templateOptions.disabled) {
formControl.disable();
}
if (!formControl.dirty && formControl.invalid && this.field.validation && !this.field.validation.show) {
formControl.markAsUntouched();
}
if (!formControl.dirty && formControl.invalid && this.field.validation && this.field.validation.show) {
formControl.markAsTouched();
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/services/formly.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class FormlyConfig {
} = {
fieldTransform: undefined,
showError: function(field: Field) {
return field.formControl.touched && !field.formControl.valid;
return (field.formControl.touched || (field.field.validation && field.field.validation.show)) && !field.formControl.valid;
},
};

Expand Down
10 changes: 6 additions & 4 deletions src/core/src/services/formly.form.builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,19 @@ describe('FormlyFormBuilder service', () => {
it('should show error when option `show` is true', () => {
field.validators = { validation: ['required'] };
field.validation = { show: true };
builder.buildForm(form, [field], {}, {});
const options: any = {};
builder.buildForm(form, [field], {}, options);

expect(form.get('title').touched).toBeTruthy();
expect(options.showError({ field: field, formControl: form })).toBeTruthy();
});

it('should not show error when option `show` is false', () => {
field.validators = { validation: ['required'] };
field.validation = { show: false };
builder.buildForm(form, [field], {}, {});
const options: any = {};
builder.buildForm(form, [field], {}, options);

expect(form.get('title').touched).toBeFalsy();
expect(options.showError({ field: field, formControl: form })).toBeFalsy();
});
});

Expand Down
3 changes: 0 additions & 3 deletions src/core/src/services/formly.form.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,6 @@ export class FormlyFormBuilder {
}

this.addControl(form, name, formControl, field);
if (field.validation && field.validation.show) {
form.get(field.key).markAsTouched();
}
}

private getValidation(opt, value) {
Expand Down

0 comments on commit 1ffe899

Please sign in to comment.