0

Is it possible to set the Angular "required" directive/attribute to other elements except input fields? I have several lists on a page where the user can check the entered data from pages before ... user should only be able to "submit" (send data to database...) if the "required" data is complete.

So how to check data in arrays which is displayed in list views? (ok, looping through arrays and check the values (is empty?) is possible - but i think there are better solutions... maybe a custom directive?)

DehMotth
  • 679
  • 3
  • 12
  • 21
  • 1
    Have you checked this http://stackoverflow.com/questions/15360094/angularjs-dropdown-required-validation – Chandermani Jul 10 '13 at 13:16
  • Checked this and it´s very helpful, but how could i do this depending on more than just one list element? I´ve added the name attribute with the same name (name="required") to more than one element, and button looks like this ng-disabled="!form.required.$valid"... but this doesn´t work, button is still disabled even when the elements are not empty... – DehMotth Jul 10 '13 at 13:41
  • Can you share some code in fiddle. `name` property of the element it used to reference the element. You do not set name=required, required is a independent attribute – Chandermani Jul 10 '13 at 13:53
  • that´s clear... it´s my first fiddle, don´t know how it´s not recognizing Angular --> http://jsfiddle.net/enND2/2 Perhaps because i´m using Jquery mobile with angular ... – DehMotth Jul 10 '13 at 14:23
  • 1
    List such as `
      ` do not have a concept of selection, you need to implement it yourself, or use html type `
    – Chandermani Jul 10 '13 at 14:38

1 Answers1

0

You can set angular validation directives on elements that have ng-model. The ng-model directive has a controller with properties like $error, $invalid, etc. So the ng-model controller acts like a storage for the errors.

If you use more exotic inputs or don't use ng-model I suggest you go "custom directive" way. I've written one such custom directive myself for such scenarios. (http://liviutrifoi.wordpress.com/2013/10/19/angular-custom-validation-with-business-rules/)

I suspect the lists you are using are bound to arrays from a controller. If that's the case you could define a "validation rule" (see my article) in the controller that iterates the arrays and returns a true/false along with an error message. Then you would put a validator directive on a list and a to display the error message. Maybe it helps.

Liviu Trifoi
  • 2,980
  • 1
  • 21
  • 28