29

I'm trying to use $setValidity on an element in a directive. All the examples I've found seem to set it on the controller...

I forked a JS fiddle on Form Validation and have tried a bunch of things. Any insights would be most appreciated: http://jsfiddle.net/thomporter/pmKpG/2/

In the fiddle, the $setValidity is called on the controller:

ctrl.$setValidity('pwd', true);

I'd like to do something like:

elm.$setValidity('pwd', true);

so that in the form I can do something like:

ng-class="{error:form.password.$error.pwd}"
0

1 Answer 1

60

I figured it out... You have to have a name on the input elements. As soon as I added the name, the errors were automatically bound to the elements as desired, no additional changes needed!

<input ng-model="password"
       name="inputPassword"
       class="immediate-help"
       password-validate
       required
       type="password"
       id="inputPassword"
       placeholder="Password">

http://jsfiddle.net/thomporter/pmKpG/4/

Sign up to request clarification or add additional context in comments.

3 Comments

@Thom Porter, Why do you return undefined instead of false line 22 (jsfiddle.net/thomporter/pmKpG/4) ?
Plus one for answering your own question - and sharing with others
@thom-porter, what is the significance of the name "pwd" in your $setValidity call? It makes no difference what I change that value to...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.