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

Commit

Permalink
feat(forms): provide support for parent form communication
Browse files Browse the repository at this point in the history
  • Loading branch information
matsko authored and mhevery committed Jan 24, 2014
1 parent d3ed15c commit 6778b62
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/directive/ng_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ part of angular.directive;
@NgDirective(
selector: 'form',
visibility: NgDirective.CHILDREN_VISIBILITY)
@NgDirective(
selector: 'fieldset',
visibility: NgDirective.CHILDREN_VISIBILITY)
@NgDirective(
selector: '.ng-form',
visibility: NgDirective.CHILDREN_VISIBILITY)
@NgDirective(
selector: '[ng-form]',
visibility: NgDirective.CHILDREN_VISIBILITY)
class NgForm extends NgControl implements NgDetachAware {
final NgForm _parentForm;
final dom.Element _element;
final Scope _scope;

Expand All @@ -23,7 +27,9 @@ class NgForm extends NgControl implements NgDetachAware {
final List<NgControl> _controls = new List<NgControl>();
final Map<String, NgControl> _controlByName = new Map<String, NgControl>();

NgForm(this._scope, this._element) {
NgForm(this._scope, dom.Element this._element, Injector injector):

This comment has been minimized.

Copy link
@bgourlie

bgourlie Jan 30, 2014

Contributor

This is the culprit for #459. Taking injector out of the constructor and commenting out references to _parentForm makes the error go away.

This comment has been minimized.

Copy link
@mhevery

mhevery Jan 31, 2014

Contributor

@matsko This should work by injecting null.

_parentForm = injector.parent.get(NgForm)
{
if(!this._element.attributes.containsKey('action')) {
this._element.onSubmit.listen((event) {
event.preventDefault();
Expand Down Expand Up @@ -59,12 +65,14 @@ class NgForm extends NgControl implements NgDetachAware {
if(currentErrors.isEmpty) {
valid = true;
}
_parentForm.setValidity(this, errorType, true);
}
}
} else {
if(queue == null) {
queue = new List<NgControl>();
currentErrors[errorType] = queue;
_parentForm.setValidity(this, errorType, false);
} else if(queue.contains(control)) {
return;
}
Expand Down
37 changes: 37 additions & 0 deletions test/directive/ng_form_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,43 @@ describe('form', () {
expect(form.valid).toBe(true);
expect(form.invalid).toBe(false);
}));

it('should set the validity for the parent form when fieldsets are used', inject((Scope scope) {
var element = $('<form name="myForm">' +
' <fieldset probe="f">' +
' <input type="text" ng-model="one" name="one" probe="m" />' +
' </fieldset>' +
'</form>');

_.compile(element);
scope.$apply();

var form = scope['myForm'];
var fieldset = _.rootScope.f.directive(NgForm);
var model = _.rootScope.m.directive(NgModel);

model.setValidity("error", false);

expect(model.valid).toBe(false);
expect(fieldset.valid).toBe(false);
expect(form.valid).toBe(false);

model.setValidity("error", true);

expect(model.valid).toBe(true);
expect(fieldset.valid).toBe(true);
expect(form.valid).toBe(true);

form.setValidity(fieldset, "error", false);
expect(model.valid).toBe(true);
expect(fieldset.valid).toBe(true);
expect(form.valid).toBe(false);

fieldset.setValidity(model, "error", false);
expect(model.valid).toBe(true);
expect(fieldset.valid).toBe(false);
expect(form.valid).toBe(false);
}));
});

describe('controls', () {
Expand Down

0 comments on commit 6778b62

Please sign in to comment.