Dynamically mark quantity field as invalid when stock is incorrect

This commit is contained in:
Matt-Yorkley
2020-05-15 11:33:41 +02:00
parent 42195b76d3
commit 3ba1182661
3 changed files with 19 additions and 2 deletions

View File

@@ -0,0 +1,17 @@
Darkswarm.directive "validateStockQuantity", ->
restrict: 'A'
require: "ngModel"
link: (scope, element, attr, ngModel) ->
ngModel.$parsers.push (selectedQuantity) ->
if parseInt(selectedQuantity) > scope.available_quantity()
ngModel.$setValidity('stock', false);
else
ngModel.$setValidity('stock', true);
selectedQuantity
scope.available_quantity = ->
on_hand = parseInt(attr.ofnOnHand)
finalized_quantity = parseInt(attr.finalizedquantity) || 0 # finalizedquantity is optional
on_hand + finalized_quantity