Angular Directive with Validation

1,396 views
Skip to first unread message

Guiboy

unread,
May 11, 2012, 8:12:09 AM5/11/12
to AngularJS
I am trying to create a directive that allows for multiple contact
fields to be inserted
i made it work as in this fiddle http://jsfiddle.net/agamennon12345/uFb3x/
for my next step i want to add custom validation to the fields based
on the combo-box filed type selection
in my effort i was trying to use a watch inside the link function. so
here are my questions:

Why does that watch does not fire upon changes into the contacts
collection?
How to watch for changes in the contacts collection?
How do i add custom validation to my fields?

Some thoughts
I know i have to add a form to the template or to the scope,
probably this could work both ways but again the details on how to
make it work eludes me
I try splashing form and validation ctrl.$parsers.unshift all
around with no success

i am reading the forums and the docs for days without avail, i just
can't wrap my head around the docs and pull this off without
assistance so any help is greatly appreciated!

Guiboy

unread,
May 11, 2012, 3:59:01 PM5/11/12
to AngularJS
i have managed to find why my $watch was not firing, i found out there
is this objectEquality flag, that will match for equality and not
reference, this fiddle now have a watch that works, mind the "true" as
third parameter for the watch
http://jsfiddle.net/agamennon12345/uFb3x/2/ unfortunately i still
need help with validation

On May 11, 9:12 am, Guiboy <gui...@gmail.com> wrote:
> I am trying to create a directive that allows for multiple contact
> fields to be inserted
> i made it work as in this fiddlehttp://jsfiddle.net/agamennon12345/uFb3x/

Vojta Jína

unread,
May 21, 2012, 1:17:07 AM5/21/12
to ang...@googlegroups.com
Have you seen http://docs.angularjs.org/guide/dev_guide.forms ?

You need a directive, that will require 'ngModel', to get hold of the
ngModel controller and will append validators there.
<select ng-name="contact.type">
<input ng-model="contact.value" custom-validator="contact.type">

You have to watch attr.customValidator as well and re-validate once it changes.

V.
> --
> You received this message because you are subscribed to the Google Groups "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to angular+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/angular?hl=en.
>

Darius Riggins

unread,
May 22, 2012, 12:29:59 PM5/22/12
to ang...@googlegroups.com
Vojta, just chiming in with a suggestion, it would be really nice if there were a way on the site to search for something like validation or validate and get results, we had a mini code-a-thon session yesterday and couldn't find this, if I was looking over each item it should of been obvious, but not everyone associates custom validation with forms.
> To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com.

Vojta Jína

unread,
Jun 11, 2012, 11:06:33 PM6/11/12
to ang...@googlegroups.com
You're right. THe whole search needs to be more clever. We already have an issue for that.

V.

To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/pDwnOcBwwyUJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages