diff --git a/www/js/controllers/transfer-controllers.js b/www/js/controllers/transfer-controllers.js index f31635e58eb682211aef8540605f71e7e6404bd3..b7bf1584ed8552c473c760ac25c4888273d96e5e 100644 --- a/www/js/controllers/transfer-controllers.js +++ b/www/js/controllers/transfer-controllers.js @@ -112,6 +112,7 @@ function TransferModalController($scope, $q, $translate, $timeout, $filter, BMA, csCurrency, csSettings, parameters) { 'ngInject'; + var minQuantitativeAmount = 0.01; $scope.convertedBalance = 0; $scope.formData = { destPub: null, @@ -121,6 +122,7 @@ function TransferModalController($scope, $q, $translate, $timeout, $filter, BMA, useComment: false }; $scope.udAmount = null; + $scope.minAmount = minQuantitativeAmount; $scope.commentPattern = BMA.regexp.COMMENT; $scope.currency = csCurrency.data.name; $scope.loading = true; @@ -181,19 +183,13 @@ function TransferModalController($scope, $q, $translate, $timeout, $filter, BMA, $scope.currency = csCurrency.data.name; if ($scope.formData.useRelative) { $scope.convertedBalance = csWallet.data.balance / csCurrency.data.currentUD; - $scope.udAmount = $scope.amount * csCurrency.data.currentUD; + $scope.minAmount = minQuantitativeAmount / (csCurrency.data.currentUD / 100); } else { $scope.convertedBalance = csWallet.data.balance; - // Convert to number - $scope.formData.amount = (!!$scope.formData.amount && typeof $scope.formData.amount == "string") ? - Math.floor(parseFloat($scope.formData.amount.replace(new RegExp('[,]'), '.'))) : - $scope.formData.amount; - // Compute UD - $scope.udAmount = (!!$scope.formData.amount && - typeof $scope.formData.amount == "number" && - !!csCurrency.data.currentUD && - typeof csCurrency.data.currentUD == "number") ? - $scope.formData.amount / csCurrency.data.currentUD :null; + $scope.minAmount = minQuantitativeAmount; + } + if ($scope.form) { + $scope.form.$valid = undefined; } }; $scope.$watch('formData.useRelative', $scope.onUseRelativeChanged, true); @@ -209,9 +205,27 @@ function TransferModalController($scope, $q, $translate, $timeout, $filter, BMA, $scope.doTransfer = function() { $scope.form.$submitted=true; + if(!$scope.form.$valid || !$scope.formData.destPub || !$scope.formData.amount) { return; } + var amount = $scope.formData.amount; + if (typeof amount === "string") { + amount = parseFloat(amount.replace(new RegExp('[.,]'), '.')); + } + + // Avoid amount less than the minimal - fix #373 + if (amount < $scope.minAmount) { + $scope.form.$valid = false; + $scope.form.amount.$invalid = true; + $scope.form.amount.$error = $scope.form.amount.$error || {}; + $scope.form.amount.$error.min = true; + return; + } + else if ($scope.form.amount.$error && $scope.form.amount.$error.min){ + $scope.form.amount.$invalid = false; + delete $scope.form.amount.$error.min; + } var currentUD; return $q.all([ @@ -230,10 +244,6 @@ function TransferModalController($scope, $q, $translate, $timeout, $filter, BMA, return UIUtils.loading.show() .then(function(){ - var amount = $scope.formData.amount; - if (typeof amount === "string") { - amount = parseFloat(amount.replace(new RegExp('[.,]'), '.')); - } if ($scope.formData.useRelative) { amount = currentUD * amount; } diff --git a/www/js/filters.js b/www/js/filters.js index abf2169aee991aa892fa7847720513d2a047a9d1..a07a9f660f69740fda231222c7e24b732cdecc1c 100644 --- a/www/js/filters.js +++ b/www/js/filters.js @@ -67,10 +67,14 @@ angular.module('cesium.filters', ['cesium.config', 'cesium.platform', 'pascalpre }) .filter('formatAmount', function(csConfig, csSettings, csCurrency, $filter) { - var minValue = 1 / Math.pow(10, csConfig.decimalCount || 4); - var format = '0,0.0' + Array(csConfig.decimalCount || 4).join('0'); + var pattern = '0,0.0' + Array(csConfig.decimalCount || 4).join('0'); + var patternBigNumber = '0,0.000 a'; var currencySymbol = $filter('currencySymbol'); + // Always add one decimal for relative unit + var patternRelative = pattern + '0'; + var minValueRelative = 1 / Math.pow(10, (csConfig.decimalCount || 4) + 1 /*add one decimal in relative*/); + function formatRelative(input, options) { var currentUD = options && options.currentUD ? options.currentUD : csCurrency.data.currentUD; if (!currentUD) { @@ -78,11 +82,11 @@ angular.module('cesium.filters', ['cesium.config', 'cesium.platform', 'pascalpre return; } var amount = input / currentUD; - if (Math.abs(amount) < minValue && input !== 0) { + if (Math.abs(input) < minValueRelative && input !== 0) { amount = '~ 0'; } else { - amount = numeral(amount).format(format); + amount = numeral(amount).format(patternRelative); } if (options && options.currency) { return amount + ' ' + currencySymbol(options.currency, true); @@ -91,7 +95,7 @@ angular.module('cesium.filters', ['cesium.config', 'cesium.platform', 'pascalpre } function formatQuantitative(input, options) { - var amount = numeral(input/100).format((input > -1000000000 && input < 1000000000) ? '0,0.00' : '0,0.000 a'); + var amount = numeral(input/100).format((input < -1000000000 || input > 1000000000) ? patternBigNumber : pattern); if (options && options.currency) { return amount + ' ' + currencySymbol(options.currency, false); } diff --git a/www/js/platform.js b/www/js/platform.js index e5db212f3824977fcbbaff1a33e6c473c4694663..e1d8e37651d89d4515d935ccf420256ba75b7099 100644 --- a/www/js/platform.js +++ b/www/js/platform.js @@ -357,5 +357,3 @@ if (typeof String.prototype.format !== 'function') { }); }; } - -; diff --git a/www/templates/wallet/transfer_form.html b/www/templates/wallet/transfer_form.html index 88323759036d4def3d8b7733ef567b52e941b132..f0c0ecce445e32dbe11f5014a8560e80a1bd7e82 100644 --- a/www/templates/wallet/transfer_form.html +++ b/www/templates/wallet/transfer_form.html @@ -48,7 +48,8 @@ <input type="text" keyboard-attach="" name="amount" placeholder="{{::'TRANSFER.AMOUNT_HELP' | translate}} ({{$root.currency.name | currencySymbolNoHtml:formData.useRelative}})" ng-model="formData.amount" - required number-float> + required + number-float> <a class="button button-stable icon ion-arrow-swap gray ink hidden-xs hidden-sm" ng-click="showUnitPopover($event)"> </a> <a class="button button-icon button-small-padding gray ink visible-xs visible-sm" ng-click="showUnitPopover($event)"> @@ -67,6 +68,9 @@ <div class="form-error" ng-message="numberInt"> <span translate="ERROR.FIELD_NOT_INT"></span> </div> + <div class="form-error" ng-message="min"> + <span translate="ERROR.FIELD_MIN" translate-values="{min: minAmount}"></span> + </div> </div> <!-- Enable comment ? -->