S
S
Sergey2017-08-01 11:37:49
Angular
Sergey, 2017-08-01 11:37:49

ng-model list validation?

Tell me how to write a validation.

<form class="ownForm" name="basket" >
<div class="ownForm-row" ng-repeat="(key, value) in form.items">
    <div class="ownForm-label">
      Продукт - <b> {{ value.name }} </b>
    </div>

    <div class="ownForm-param">
      <input type="text" class="form-control" ng-model="value.quantity" name="quantity[{{key}}]" ng-pattern="/^[1-9]\d*$/" required>

      <button class="btn btn-danger" ng-click="deleteItem(key)">Удалить</button>

      <div ng-show="basket.$submitted &&  basket.quantity[key].$invalid"  class="ownForm-error">
        <span ng-show="basket.quantity[key].$error.required"> Это обязательное поле </span>
        <span ng-show="basket.quantity[key].$error.pattern"> Введите положительное число </span>
      </div>
    </div>
  </div>
</form>

The validator says that quantity[19] is pattern-matching, but this line doesn't pass basket.quantity[key].$invalid

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2017-08-01
@Sergamers

Solved a problem. The bottom line is that he considered quantity[19] to be a string, not an array. Solved the problem like this:

<input type="text" class="form-control" ng-model="value.quantity" name="quantity-{{key}}" ng-pattern="/^[1-9]\d*$/" required>

<div ng-show="basket.$submitted && basket['quantity-' + key].$invalid" class="ownForm-error">
        <span ng-show="basket['quantity-' + key].$error.required"> Это обязательное поле </span>
        <span ng-show="basket['quantity-' + key].$error.pattern"> Введите положительное число </span>
      </div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question