2

I created a directive which is made by 2 selects, that loads the options from 2 json file:

http://plnkr.co/edit/7kK66c?p=preview

As you can see it works fine, but I would like to put this directive into a form, and check its validity. To be more specific I would like this directive to be valid only if the user selected an option from both selects (just like both have been marked with required). I found fields of tutorial about how to set the validy of directive that contains input text, but I nothing to check on selects...how could I do that?

user1012480
  • 752
  • 2
  • 11
  • 24
  • http://stackoverflow.com/questions/12581439/how-to-add-custom-validation-to-an-angular-js-form – lucuma May 14 '13 at 14:57

1 Answers1

0

Have a look at templateDir.html on this Plunker for a solution.

You would need to:

  • Add a <form></form> wrapper with a name="..." attribute.
  • Add an ng-model="..." attribute to each of the <select></select> tags.
  • Add ng-required="true" to each of the <select></select> tags.

You can then check whether or not the form is in a valid state using formName.$invalid.

The ng-model tells Angular to bind the values of those inputs to the variable name you specify in your model and the ng-required tells Angular that those two inputs must be completed. I'm assuming the purpose of <form></form> is obvious. :)

Michael Moussa
  • 4,207
  • 5
  • 35
  • 53