Permalink
Browse files

feat(wallet): transaction fee validation change

jQuery validation mechanism didn't work well with jQuery.ui autocomplete,
so I had to implement a the field checks manually.
Added precision as a parameter for decimals validation routine.
  • Loading branch information...
1 parent 4cc44a6 commit 3d2f4d2e28818cd8363e7392b5b494507fb3b698 @beregovoy68 beregovoy68 committed Jul 27, 2016
Showing with 54 additions and 21 deletions.
  1. +6 −2 css/style.css
  2. +1 −1 js/waves.money.js
  3. +6 −3 js/waves.ui.js
  4. +41 −15 js/waves.ui.wallet.js
View
@@ -1218,8 +1218,12 @@ input.tabs-radio:checked + label img {
top: 0;
bottom: 0;
right: 0;
- margin-right: -.25rem;
- padding: 0;
+ margin: 2px;
+ border-style: none none none solid;
+}
+
+.custom-combobox-toggle:hover {
+ border-style: none none none solid;
}
.custom-combobox-input {
View
@@ -99,7 +99,7 @@ var Money = function(amount, currency) {
this.formatAmount = function (stripZeroes) {
if (stripZeroes)
- return this.amount.toString();
+ return this.toTokens().toString();
return format(this.amount);
}
View
@@ -552,9 +552,12 @@ var Waves = (function(Waves, $, undefined) {
$.validator.addMethod('address', function(value, element){
return this.optional(element) || Waves.Addressing.validateDisplayAddress(value);
}, "Account number must be a sequence of 35 alphanumeric characters with no spaces, optionally starting with '1W'");
- $.validator.addMethod('decimal', function(value, element) {
- return this.optional(element) || /^(?:-?\d+)?(?:\.\d+)?$/.test(value);
- }, "Amount is expected with a dot (.) as a decimal separator");
+ $.validator.addMethod('decimal', function(value, element, params) {
+ var maxdigits = $.isNumeric(params) ? params : Waves.UI.constants.AMOUNT_DECIMAL_PLACES;
+
+ var regex = new RegExp("^(?:-?\\d+)?(?:\\.\\d{1," + maxdigits + "})?$");
+ return this.optional(element) || regex.test(value);
+ }, "Amount is expected with a dot (.) as a decimal separator with no more than {0} fraction digits");
$.validator.addMethod('password', function(value, element){
if (this.optional(element))
return true;
View
@@ -44,28 +44,25 @@ var Waves = (function(Waves, $, undefined) {
Waves.UI.sendWavesForm = {
id : 'send-waves-form',
containerId : 'wB-butSend-WAV',
+ feeInputSelector : '.custom-combobox-input',
+ errorClass : 'wInput-error',
validator : undefined,
getForm : function() {
return $('#' + Waves.UI.sendWavesForm.id);
},
setupValidation : function() {
$('#' + this.containerId).on($.modal.BEFORE_OPEN, function(event, modal) {
Waves.UI.sendWavesForm.validator = Waves.UI.sendWavesForm.getForm().validate({
- errorClass : 'wInput-error',
+ errorClass : Waves.UI.sendWavesForm.errorClass,
rules : {
wavesrecipient : {
required : true,
address : true
},
wavessendamount : {
required : true,
- decimal : true,
+ decimal : Currency.WAV.precision,
min : Waves.UI.constants.MINIMUM_PAYMENT_AMOUNT
- },
- wavessendfee : {
- required : true,
- decimal : true,
- min : Waves.UI.constants.MINIMUM_TRANSACTION_FEE
}
},
messages : {
@@ -74,15 +71,9 @@ var Waves = (function(Waves, $, undefined) {
},
wavessendamount : {
required : 'Amount to send is required',
- decimal : 'Amount to send must be a decimal number with dot (.) as a decimal separator',
+ decimal : 'Amount to send must be a decimal number with dot (.) as a decimal separator with no more than {0} fraction digits',
min : 'Payment amount is too small. It should be greater or equal to ' +
Waves.UI.constants.MINIMUM_PAYMENT_AMOUNT.toFixed(Waves.UI.constants.AMOUNT_DECIMAL_PLACES)
- },
- wavessendfee : {
- required : 'Transaction fee is required',
- decimal : 'Transaction fee must be a decimal number with dot (.) as a decimal separator',
- min : 'Transactions fee is too small. It should be greater or equal to ' +
- Waves.UI.constants.MINIMUM_TRANSACTION_FEE
}
}
});
@@ -91,11 +82,46 @@ var Waves = (function(Waves, $, undefined) {
$('#' + this.containerId).on($.modal.BEFORE_CLOSE, function(event, modal) {
if (Waves.UI.sendWavesForm.validator !== undefined)
Waves.UI.sendWavesForm.validator.resetForm();
+
+ $(this.feeInputSelector).parent().removeClass(this.errorClass);
});
},
isValid : function() {
- return this.getForm().valid();
+ if (!this.getForm().valid())
+ return false;
+
+ // manually validate tx fee cos jquery.validation conflicts with autocomplete.combobox
+ var element = $(this.feeInputSelector);
+ var value = element.val();
+
+ if (!$.validator.methods.required.call(this.validator, value, element[0], true)) {
+ $.growl.error({ message: 'Transaction fee is required' });
+ element.parent().addClass(this.errorClass);
+
+ return false;
+ }
+
+ if (!$.validator.methods.min.call(this.validator, value, element[0], Waves.UI.constants.MINIMUM_TRANSACTION_FEE)) {
+ $.growl.error({ message: 'Transaction fee is too small. It should be greater or equal to ' +
+ Waves.UI.constants.MINIMUM_TRANSACTION_FEE });
+ element.parent().addClass(this.errorClass);
+
+ return false;
+ }
+
+ if (!$.validator.methods.decimal.call(this.validator, value, element[0], Currency.WAV.precision)) {
+ $.growl.error({ message: 'Transaction fee must be a decimal number with dot (.) as ' +
+ 'a decimal separator with no more than ' + Currency.WAV.precision +
+ ' fraction digits' });
+ element.parent().addClass(this.errorClass);
+
+ return false;
+ }
+
+ element.parent().removeClass(this.errorClass);
+
+ return true;
}
};

0 comments on commit 3d2f4d2

Please sign in to comment.