Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 7a10390

Browse files
matskorkirov
authored andcommitted
feat(ngForm): allow forms to be manually validated
1 parent 6a6e4ca commit 7a10390

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

lib/directive/ng_control.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ abstract class NgControl implements AttachAware, DetachAware {
5252
_parentControl..removeStates(this)..removeControl(this);
5353
}
5454

55+
/**
56+
* Revalidates the model by performing validations on each of the child controls.
57+
*/
58+
void validate() {
59+
_controls.forEach((control) {
60+
control.validate();
61+
});
62+
}
63+
5564
/**
5665
* Resets the form and inner models to their pristine state.
5766
*/
@@ -339,7 +348,9 @@ class NgNullControl implements NgControl {
339348
void addInfoState(NgControl control, String state) {}
340349
void removeInfoState(NgControl control, String state) {}
341350

351+
void validate() {}
342352
void reset() {}
353+
343354
void attach() {}
344355
void detach() {}
345356

test/directive/ng_form_spec.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,54 @@ void main() {
553553
expect(form).not.toHaveClass('ng-required-invalid');
554554
});
555555

556+
it('should validate all the models when form.validate() is called',
557+
(TestBed _, Scope scope) {
558+
559+
scope.context['required'] = true;
560+
var formElement = _.compile('<form name="myForm">'
561+
'<input type="text" ng-model="one" probe="a" required />'
562+
'<input type="text" ng-model="two" probe="b" required />'
563+
'<input type="text" ng-model="three" probe="c" required />'
564+
'</form>');
565+
scope.apply();
566+
567+
NgForm form = _.rootScope.context['myForm'];
568+
569+
var one = _.rootScope.context['a'].directive(NgModel);
570+
var inputOne = one.element.node;
571+
572+
var two = _.rootScope.context['b'].directive(NgModel);
573+
var inputTwo = two.element.node;
574+
575+
var three = _.rootScope.context['c'].directive(NgModel);
576+
var inputThree = three.element.node;
577+
578+
expect(form.invalid).toBe(true);
579+
expect(one.invalid).toBe(true);
580+
expect(two.invalid).toBe(true);
581+
expect(three.invalid).toBe(true);
582+
583+
one.viewValue = 'something';
584+
expect(one.invalid).toBe(true);
585+
586+
form.validate();
587+
588+
expect(one.invalid).toBe(false);
589+
expect(two.invalid).toBe(true);
590+
expect(three.invalid).toBe(true);
591+
expect(form.invalid).toBe(true);
592+
593+
two.viewValue = 'something else';
594+
three.viewValue = 'something more';
595+
596+
form.validate();
597+
598+
expect(one.invalid).toBe(false);
599+
expect(two.invalid).toBe(false);
600+
expect(three.invalid).toBe(false);
601+
expect(form.invalid).toBe(false);
602+
});
603+
556604
it('should re-validate itself when validators are toggled on and off',
557605
(TestBed _, Scope scope) {
558606

0 commit comments

Comments
 (0)