mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-13 23:37:47 +00:00
Do not allow invalid quantity to reach model, triggering server update
This commit is contained in:
@@ -1,9 +1,19 @@
|
||||
Darkswarm.directive "ofnOnHand", ->
|
||||
restrict: 'A'
|
||||
link: (scope, elem, attr) ->
|
||||
on_hand = parseInt(attr.ofnOnHand)
|
||||
elem.bind 'change', (e) ->
|
||||
if parseInt(elem.val()) > on_hand
|
||||
scope.$apply ->
|
||||
alert t('insufficient_stock', {on_hand: on_hand})
|
||||
elem.val(on_hand)
|
||||
require: "ngModel"
|
||||
|
||||
link: (scope, elem, attr, ngModel) ->
|
||||
# In cases where this field gets its value from the HTML element rather than the model,
|
||||
# initialise the model with the HTML value.
|
||||
if scope.$eval(attr.ngModel) == undefined
|
||||
ngModel.$setViewValue elem.val()
|
||||
|
||||
ngModel.$parsers.push (viewValue) ->
|
||||
on_hand = parseInt(attr.ofnOnHand)
|
||||
if parseInt(viewValue) > on_hand
|
||||
alert t('insufficient_stock', {on_hand: on_hand})
|
||||
viewValue = on_hand
|
||||
ngModel.$setViewValue viewValue
|
||||
ngModel.$render()
|
||||
|
||||
viewValue
|
||||
|
||||
Reference in New Issue
Block a user