Author: bleny Date: 2013-03-27 16:41:29 +0100 (Wed, 27 Mar 2013) New Revision: 52 Url: http://forge.codelutin.com/projects/franciaflex-magalie/repository/revisions... Log: use exception handling for validation in withdraw item screen Modified: trunk/magalie-web/src/main/webapp/js/withdraw-item-input.js Modified: trunk/magalie-web/src/main/webapp/js/withdraw-item-input.js =================================================================== --- trunk/magalie-web/src/main/webapp/js/withdraw-item-input.js 2013-03-27 14:56:51 UTC (rev 51) +++ trunk/magalie-web/src/main/webapp/js/withdraw-item-input.js 2013-03-27 15:41:29 UTC (rev 52) @@ -33,6 +33,18 @@ return processing; } +function ValidationError(message) { + this.message = message; +} + +ValidationError.prototype = new Error(); +ValidationError.prototype.constructor = ValidationError; + +function handleValidationError(validationError) { + alert(validationError.message); + console.debug(validationError); +} + "use strict"; model.storageMovementsIndex = 0; @@ -68,7 +80,7 @@ } } if (storageMovement == null) { - alert(barcode + " n'est pas le code barre d'un emplacement valide"); + throw new ValidationError(barcode + " n'est pas le code barre d'un emplacement valide"); } storageMovement.used = true; storageMovement.defect = quantity != storageMovement.quantity; @@ -151,24 +163,34 @@ onNext : function() { - if (model.allowSubmit) { + try { - } else { + if (model.allowSubmit) { - var barcode = $('#siteBarcode').val(); - var quantity = parseInt($('#quantity').val()); - if ( ! isFinite(quantity) || quantity < 0) { - alert('Il faut entrer une quantité valide'); - return; + } else { + + var barcode = $('#siteBarcode').val(); + var quantity = parseInt($('#quantity').val()); + if ( ! isFinite(quantity) || quantity < 0) { + throw new ValidationError('Il faut entrer une quantité strictement supérieure à 0'); + } + + model.withdraw(barcode, quantity); + + // if used site focused as current, highlight next site to go + if (barcode == model.storageMovements[model.storageMovementsIndex].originSite.barcode) { + model.nextSite(); + } + } - model.withdraw(barcode, quantity); + } catch (ex) { - // if used site focused as current, highlight next site to go - if (barcode == model.storageMovements[model.storageMovementsIndex].originSite.barcode) { - model.nextSite(); + if (ex instanceof ValidationError) { + handleValidationError(ex); + } else { + throw ex; } - } },