Extending ngFormController & ngModelController

244 views
Skip to first unread message

br...@facultycreative.com

unread,
Oct 18, 2013, 5:02:18 PM10/18/13
to ang...@googlegroups.com
I'd like to extend ngForm & ngModel to shift the responsibility for validations onto the model, such that a mock object would look like:

user = {
      name: '',
      email: '',
      zip: '',
      id: '',
      validates: {
        name:  { presence: true },
        email: { presence: true, email: true },
        zip:   { presence: true, zip: [ zipValidator, "Must contain a valid zip code" ] }
      },
      save: angular.noop,
      find: angular.noop
    };

The test suite I've written passes fine when I actually go in and flat out change Angular, but I'd really prefer to shift to leave Angular itself alone. I tried moving off my work to form and ngModel directives that require form and ngModel's controllers, but when Angular evaluates the template, it calls ng-model, which calls formCtrl.$addControl before my directives have an opportunity to load, and thus my extensions never happen. 

Anyone know of any way to do this? Here's a gist of the changes I'm making:


Best,
Brett

br...@facultycreative.com

unread,
Oct 18, 2013, 7:19:26 PM10/18/13
to ang...@googlegroups.com
Figured it out. The compile function does the trick. Cheers!

Bretto

unread,
Oct 19, 2013, 1:29:21 AM10/19/13
to ang...@googlegroups.com
Could you share your solution in a jsfiddle I am working on a form generation with breeze and the model is similar...
Reply all
Reply to author
Forward
0 new messages