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

Commit

Permalink
feat(ngForm): allow forms to be manually validated
Browse files Browse the repository at this point in the history
  • Loading branch information
matsko authored and rkirov committed Jan 14, 2015
1 parent 6a6e4ca commit 7a10390
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/directive/ng_control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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() {}

Expand Down
48 changes: 48 additions & 0 deletions test/directive/ng_form_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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('<form name="myForm">'
'<input type="text" ng-model="one" probe="a" required />'
'<input type="text" ng-model="two" probe="b" required />'
'<input type="text" ng-model="three" probe="c" required />'
'</form>');
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) {

Expand Down

0 comments on commit 7a10390

Please sign in to comment.