1

I'm using Ionic and I'm using ng-repeat for multiple forms. I'd like to have separate validation for each form, but I'm not sure how I could make this work.

The code:

<ion-list ng-if="question.QuestionType == 'QuestionRadio'">
    <ion-radio ng-repeat="choice in question.Answers"
               ng-model="vm.answer[choice.AnswerId]"
               name="{{question.QuestionId}}"
               ng-required="vm.answer"
               ng-required="!vm.someSelected(vm.answer)">
        {{choice.Text}}
    </ion-radio>
</ion-list>

The code to show error message:

<div class="form-errors" 
       ng-show="testForm[question.QuestionId].$error.required"
       ng-messages="testForm[question.QuestionId].$error"
       ng-messages-include="shared/form-errors.html"
       ng-if="question.QuestionType == 'QuestionRadio'">
</div>

The function to make validation work, but not for separate forms:

vm.someSelected = function (object) {
          return Object.keys(object).some(function (key) {
            return object[key];
          });
      }

Combining the code above will make the form have validation, but if one form passes, all of them will pass, which is an undesired result. How do I make it so a form has a unique way of passing validation?

Rodia
  • 1,407
  • 8
  • 22
  • 29
J. Doe
  • 65
  • 4

1 Answers1

1

Duplicate attributes on an element are ignored:

<ion-list ng-if="question.QuestionType == 'QuestionRadio'">
    <ion-radio ng-repeat="choice in question.Answers"
               ng-model="vm.answer[choice.AnswerId]"
               name="{{question.QuestionId}}"
               ̶n̶g̶-̶r̶e̶q̶u̶i̶r̶e̶d̶=̶"̶v̶m̶.̶a̶n̶s̶w̶e̶r̶"̶
               ̶n̶g̶-̶r̶e̶q̶u̶i̶r̶e̶d̶=̶"̶!̶v̶m̶.̶s̶o̶m̶e̶S̶e̶l̶e̶c̶t̶e̶d̶(̶v̶m̶.̶a̶n̶s̶w̶e̶r̶)̶"̶
               ng-required="vm.answer[choice.AnswerId] && !vm.someSelected(vm.answer)">
        {{choice.Text}}
    </ion-radio>
</ion-list>
georgeawg
  • 48,608
  • 13
  • 72
  • 95