Reputation: 1529
I have this form:
<div ng-repeat="item in ctrl.cartItems">
<div class="col-sm-4 cartItemLabel" ng-bind-html="item.label"> </div>
<div class="col-sm-2 inputItem">
<input class="form-control input" type="text" id="item.id" name="item.id" ng-change="ctrl.updateSub(item)" ng-model="item.qty" max="item.maxqty">
</div>
<div class="col-sm-1 inputItemValue">
<span ng-bind="item.maxqty"></span>
</div>
<div class="col-sm-2 inputItemValue">
<span ng-bind="item.value | currency:USD$:2"></span>
</div>
<div class="col-sm-1 inputItemValue">
$
</div>
<div class="col-sm-2 inputItem">
<input class="form-control" type="text" id="item.id" name="item.id" ng-model="item.subTotal">
</div>
<div class="col-sm-4"></div>
<div class="col-sm-2 error" ng-show="form.item.id.$invalid">
<small class="error" ng-show="form.item.id.$error.max">
You exceeded the maximum allowed
</small>
</div>
</div>
The value for max is set to item.maxqty, which is pulled from JSON in my model. All values are populated correctly. I just can't seem to get the validation to work. Any ideas? The important bit of the code is deep in the bushes :) So, here is the line I need to validate:
<input class="form-control input" type="text" id="item.id" name="item.id" ng-change="ctrl.updateSub(item)" ng-model="item.qty" max="item.maxqty">
Upvotes: 0
Views: 10473
Reputation: 2778
You have a max in an input type="text". It ll not work if it´s not a number one. Is that?
Must be
<input class="form-control input" type="number" id="item.id" name="item.id" ng-change="ctrl.updateSub(item)" ng-model="item.qty" max="item.maxqty">
EDIT Also, the item $valid is ok for that
<div class="col-sm-2" ng-hide="form.item.id.$valid" >
<small class="error" ng-show="form.item.id.$error.max">You exceeded the maximum </small>
</div>
anyway, you´ll not see that validation until the model is bigger, i supose (the input will not allow you to enter a number bigger than the max) check this fiddle in plnkr
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-number-input-directive-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.0-rc.0/angular.min.js"></script>
</head>
<body ng-app="numberExample">
<script>
angular.module('numberExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.example = {
value: 12
};
}]);
</script>
<form name="myForm" ng-controller="ExampleController">
<label>Number
<input type="number" name="input" ng-model="example.value"
min="0" max="10" required>
</label>
<div role="alert">
<span class="error" ng-show="myForm.input.$error.required">
Required!</span>
<span class="error" ng-show="myForm.input.$error.number">
Not valid number!</span>
<span class="error" ng-show="myForm.input.$error.max">
THIS IS A BIG NUMBER!</span>
</div>
<h1> PLEASE TRY TO PUT A NUMBER BIGGER THAN 10</h1>
<tt>value = {{example.value}}</tt><br/>
<tt>myForm.input.max = {{myForm.input.$max}}</tt><br/>
<tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/>
<tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/>
<tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
<tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
</form>
</body>
</html>
Upvotes: 2