Michal Kucaj
Michal Kucaj

Reputation: 691

Angular, getting Ng-required of checked checkbox (or radio) in ng-repeat

I would like to validate a form with multiple radio check boxes inside ng-repeat. How should I validate the selected option with radio button input:
Validation outside the ng-repeat works fine. For eg. when switching $index to customDish. The problem is validation inside ng-repeat with $index.

Field Required should be shown exactly to the selected radio button inside ng-repeat loop.

Here is a plunker

[http://plnkr.co/edit/j6bswtctD0ixaQmcDrp1?p=preview][1]

Please help
Thanks in advance

  [1]: http://plnkr.co/edit/j6bswtctD0ixaQmcDrp1?p=preview

Upvotes: 1

Views: 912

Answers (2)

Michal Kucaj
Michal Kucaj

Reputation: 691

Another Solution

 <input ng-pattern="onlyNumbers" placeholder="quantity" ng-required="$parent.checkboxSelection === {{$index}}" name="dishQuantity{{$index}}" id="dishPrice" data-ng-model="dish.quantity" type="text" class="dish-quantity" id="dishQuantity" /> 

and span tag

<span class="custom-label-danger" ng-show="submitClicked && myOrderForm.dishQuantity{{$index}}.$error.required ">Field Required</span>

Also working :)

Upvotes: 0

Marcus
Marcus

Reputation: 341

Just compare this with your's ng-show:

ng-show="submitClicked && myOrderForm.dishQuantity.$error.required && 
($parent.checkboxSelection == $index)"

I've added one more condition, ensuring, that error will show only for selected item.

Upvotes: 1

Related Questions