diff --git a/lib/directive/ng_control.dart b/lib/directive/ng_control.dart index bc0bb4524..1922efe31 100644 --- a/lib/directive/ng_control.dart +++ b/lib/directive/ng_control.dart @@ -52,6 +52,15 @@ abstract class NgControl implements AttachAware, DetachAware { _parentControl..removeStates(this)..removeControl(this); } + /** + * Revalidates the model by performing validations on each of the child controls. + */ + void validate() { + _controls.forEach((control) { + control.validate(); + }); + } + /** * Resets the form and inner models to their pristine state. */ @@ -339,7 +348,9 @@ class NgNullControl implements NgControl { void addInfoState(NgControl control, String state) {} void removeInfoState(NgControl control, String state) {} + void validate() {} void reset() {} + void attach() {} void detach() {} diff --git a/test/directive/ng_form_spec.dart b/test/directive/ng_form_spec.dart index f32716d23..f58a8e2d7 100644 --- a/test/directive/ng_form_spec.dart +++ b/test/directive/ng_form_spec.dart @@ -553,6 +553,54 @@ void main() { expect(form).not.toHaveClass('ng-required-invalid'); }); + it('should validate all the models when form.validate() is called', + (TestBed _, Scope scope) { + + scope.context['required'] = true; + var formElement = _.compile('
'); + scope.apply(); + + NgForm form = _.rootScope.context['myForm']; + + var one = _.rootScope.context['a'].directive(NgModel); + var inputOne = one.element.node; + + var two = _.rootScope.context['b'].directive(NgModel); + var inputTwo = two.element.node; + + var three = _.rootScope.context['c'].directive(NgModel); + var inputThree = three.element.node; + + expect(form.invalid).toBe(true); + expect(one.invalid).toBe(true); + expect(two.invalid).toBe(true); + expect(three.invalid).toBe(true); + + one.viewValue = 'something'; + expect(one.invalid).toBe(true); + + form.validate(); + + expect(one.invalid).toBe(false); + expect(two.invalid).toBe(true); + expect(three.invalid).toBe(true); + expect(form.invalid).toBe(true); + + two.viewValue = 'something else'; + three.viewValue = 'something more'; + + form.validate(); + + expect(one.invalid).toBe(false); + expect(two.invalid).toBe(false); + expect(three.invalid).toBe(false); + expect(form.invalid).toBe(false); + }); + it('should re-validate itself when validators are toggled on and off', (TestBed _, Scope scope) {