Skip to content

Commit 8742432

Browse files
Dzmitry Shylovichvicb
Dzmitry Shylovich
authored andcommitted
feat(forms): add option to use browser's native validation and angular forms (#13566)
Also renames NgNovalidate -> NgNoValidate Closes #13573
1 parent 551fe50 commit 8742432

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

modules/@angular/forms/src/directives.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {NgControlStatus, NgControlStatusGroup} from './directives/ng_control_sta
1414
import {NgForm} from './directives/ng_form';
1515
import {NgModel} from './directives/ng_model';
1616
import {NgModelGroup} from './directives/ng_model_group';
17-
import {NgNovalidate} from './directives/ng_novalidate_directive';
17+
import {NgNoValidate} from './directives/ng_no_validate_directive';
1818
import {NumberValueAccessor} from './directives/number_value_accessor';
1919
import {RadioControlValueAccessor} from './directives/radio_control_value_accessor';
2020
import {RangeValueAccessor} from './directives/range_value_accessor';
@@ -45,7 +45,7 @@ export {NgSelectOption, SelectControlValueAccessor} from './directives/select_co
4545
export {NgSelectMultipleOption, SelectMultipleControlValueAccessor} from './directives/select_multiple_control_value_accessor';
4646

4747
export const SHARED_FORM_DIRECTIVES: Type<any>[] = [
48-
NgNovalidate,
48+
NgNoValidate,
4949
NgSelectOption,
5050
NgSelectMultipleOption,
5151
DefaultValueAccessor,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Directive} from '@angular/core';
10+
11+
/**
12+
* @whatItDoes Adds `novalidate` attribute to all forms by default.
13+
*
14+
* `novalidate` is used to disable browser's native form validation.
15+
*
16+
* If you want to use native validation with Angular forms, just add `ngNativeValidate` attribute:
17+
*
18+
* ```
19+
* <form ngNativeValidate></form>
20+
* ```
21+
*
22+
* @experimental
23+
*/
24+
@Directive({
25+
selector: 'form:not([ngNoForm]):not([ngNativeValidate])',
26+
host: {'novalidate': ''},
27+
})
28+
export class NgNoValidate {
29+
}

modules/@angular/forms/src/directives/ng_novalidate_directive.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

modules/@angular/forms/test/template_integration_spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ export function main() {
9797
expect(form.nativeElement.getAttribute('novalidate')).toEqual('');
9898
}));
9999

100+
it('should be possible to use native validation and angular forms', fakeAsync(() => {
101+
const fixture = initTest(NgModelNativeValidateForm);
102+
103+
fixture.detectChanges();
104+
tick();
105+
106+
const form = fixture.debugElement.query(By.css('form'));
107+
expect(form.nativeElement.hasAttribute('novalidate')).toEqual(false);
108+
}));
109+
100110
it('should support ngModelGroup', fakeAsync(() => {
101111
const fixture = initTest(NgModelGroupForm);
102112
fixture.componentInstance.first = 'Nancy';
@@ -258,7 +268,7 @@ export function main() {
258268
const fixture = initTest(NgNoFormComp);
259269
fixture.detectChanges();
260270
const form = fixture.debugElement.query(By.css('form'));
261-
expect(form.nativeElement.hasAttribute('novalidate')).toBeFalsy();
271+
expect(form.nativeElement.hasAttribute('novalidate')).toEqual(false);
262272
});
263273
});
264274

@@ -1222,6 +1232,10 @@ class NgModelForm {
12221232
onReset() {}
12231233
}
12241234

1235+
@Component({selector: 'ng-model-native-validate-form', template: `<form ngNativeValidate></form>`})
1236+
class NgModelNativeValidateForm {
1237+
}
1238+
12251239
@Component({
12261240
selector: 'ng-model-group-form',
12271241
template: `

0 commit comments

Comments
 (0)