mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-18 05:51:34 +02:00
CW-243-Better-error-message-for-too-many-decimals (#818)
* update amoung validation * fix decimal validator * minor fix
This commit is contained in:
@@ -1,32 +1,59 @@
|
||||
import 'package:cake_wallet/core/validator.dart';
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
|
||||
class AmountValidator extends TextValidator {
|
||||
AmountValidator({required WalletType type, bool isAutovalidate = false})
|
||||
AmountValidator({required CryptoCurrency currency, bool isAutovalidate = false}) {
|
||||
symbolsAmountValidator =
|
||||
SymbolsAmountValidator(isAutovalidate: isAutovalidate);
|
||||
decimalAmountValidator = DecimalAmountValidator(currency: currency);
|
||||
}
|
||||
|
||||
late final SymbolsAmountValidator symbolsAmountValidator;
|
||||
|
||||
late final DecimalAmountValidator decimalAmountValidator;
|
||||
|
||||
String? call(String? value) => symbolsAmountValidator(value) ?? decimalAmountValidator(value);
|
||||
}
|
||||
|
||||
class SymbolsAmountValidator extends TextValidator {
|
||||
SymbolsAmountValidator({required bool isAutovalidate})
|
||||
: super(
|
||||
errorMessage: S.current.error_text_amount,
|
||||
pattern: _pattern(type),
|
||||
errorMessage: S.current.error_text_amount,
|
||||
pattern: _pattern(),
|
||||
isAutovalidate: isAutovalidate,
|
||||
minLength: 0,
|
||||
maxLength: 0);
|
||||
|
||||
static String _pattern() => '^([0-9]+([.\,][0-9]+)?|[.\,][0-9]+)\$';
|
||||
}
|
||||
|
||||
class DecimalAmountValidator extends TextValidator {
|
||||
DecimalAmountValidator({required CryptoCurrency currency, bool isAutovalidate = false})
|
||||
: super(
|
||||
errorMessage: S.current.decimal_places_error,
|
||||
pattern: _pattern(currency),
|
||||
isAutovalidate: isAutovalidate,
|
||||
minLength: 0,
|
||||
maxLength: 0);
|
||||
|
||||
static String _pattern(WalletType type) {
|
||||
switch (type) {
|
||||
case WalletType.monero:
|
||||
return '^([0-9]+([.\,][0-9]{0,12})?|[.\,][0-9]{1,12})\$';
|
||||
case WalletType.bitcoin:
|
||||
return '^([0-9]+([.\,][0-9]{0,8})?|[.\,][0-9]{1,8})\$';
|
||||
static String _pattern(CryptoCurrency currency) {
|
||||
switch (currency) {
|
||||
case CryptoCurrency.xmr:
|
||||
return '^([0-9]+([.\,][0-9]{1,12})?|[.\,][0-9]{1,12})\$';
|
||||
case CryptoCurrency.btc:
|
||||
return '^([0-9]+([.\,][0-9]{1,8})?|[.\,][0-9]{1,8})\$';
|
||||
default:
|
||||
return '';
|
||||
return '^([0-9]+([.\,][0-9]{1,12})?|[.\,][0-9]{1,12})\$';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class AllAmountValidator extends TextValidator {
|
||||
AllAmountValidator() : super(
|
||||
errorMessage: S.current.error_text_amount,
|
||||
pattern: S.current.all,
|
||||
minLength: 0,
|
||||
maxLength: 0);
|
||||
AllAmountValidator()
|
||||
: super(
|
||||
errorMessage: S.current.error_text_amount,
|
||||
pattern: S.current.all,
|
||||
minLength: 0,
|
||||
maxLength: 0);
|
||||
}
|
||||
|
||||
@@ -240,7 +240,7 @@ class ExchangePage extends BasePage {
|
||||
.bodyText1!
|
||||
.color!,
|
||||
currencyValueValidator: AmountValidator(
|
||||
type: exchangeViewModel.wallet.type),
|
||||
currency: exchangeViewModel.depositCurrency),
|
||||
addressTextFieldValidator: AddressValidator(
|
||||
type: exchangeViewModel.depositCurrency),
|
||||
onPushPasteButton: (context) async {
|
||||
@@ -304,7 +304,7 @@ class ExchangePage extends BasePage {
|
||||
.bodyText1!
|
||||
.decorationColor!,
|
||||
currencyValueValidator: AmountValidator(
|
||||
type: exchangeViewModel.wallet.type),
|
||||
currency: exchangeViewModel.receiveCurrency),
|
||||
addressTextFieldValidator:
|
||||
AddressValidator(
|
||||
type: exchangeViewModel
|
||||
|
||||
@@ -165,7 +165,7 @@ class ExchangeTemplatePage extends BasePage {
|
||||
.bodyText1!
|
||||
.color!,
|
||||
currencyValueValidator: AmountValidator(
|
||||
type: exchangeViewModel.wallet.type),
|
||||
currency: exchangeViewModel.depositCurrency),
|
||||
//addressTextFieldValidator: AddressValidator(
|
||||
// type: exchangeViewModel.depositCurrency),
|
||||
),
|
||||
@@ -206,7 +206,7 @@ class ExchangeTemplatePage extends BasePage {
|
||||
.bodyText1!
|
||||
.decorationColor!,
|
||||
currencyValueValidator: AmountValidator(
|
||||
type: exchangeViewModel.wallet.type),
|
||||
currency: exchangeViewModel.receiveCurrency),
|
||||
//addressTextFieldValidator: AddressValidator(
|
||||
// type: exchangeViewModel.receiveCurrency),
|
||||
)),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:cake_wallet/routes.dart';
|
||||
import 'package:cake_wallet/utils/show_bar.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
import 'package:device_display_brightness/device_display_brightness.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
@@ -119,7 +120,8 @@ class QRWidget extends StatelessWidget {
|
||||
hintText: S.of(context).receive_amount,
|
||||
textColor: Theme.of(context).accentTextTheme!.headline2!.backgroundColor!,
|
||||
borderColor: Theme.of(context).textTheme!.headline5!.decorationColor!,
|
||||
validator: AmountValidator(type: addressListViewModel.type, isAutovalidate: true),
|
||||
validator: AmountValidator(currency:
|
||||
walletTypeToCryptoCurrency(addressListViewModel.type), isAutovalidate: true),
|
||||
// FIX-ME: Check does it equal to autovalidate: true,
|
||||
autovalidateMode: AutovalidateMode.always,
|
||||
placeholderTextStyle: TextStyle(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:cake_wallet/view_model/send/output.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/entities/template.dart';
|
||||
import 'package:cake_wallet/store/templates/send_template_store.dart';
|
||||
@@ -26,7 +27,8 @@ abstract class SendTemplateViewModelBase with Store {
|
||||
|
||||
Output output;
|
||||
|
||||
Validator get amountValidator => AmountValidator(type: _wallet.type);
|
||||
Validator get amountValidator =>
|
||||
AmountValidator(currency: walletTypeToCryptoCurrency(_wallet.type));
|
||||
|
||||
Validator get addressValidator => AddressValidator(type: _wallet.currency);
|
||||
|
||||
|
||||
@@ -126,7 +126,8 @@ abstract class SendViewModelBase with Store {
|
||||
|
||||
CryptoCurrency get currency => _wallet.currency;
|
||||
|
||||
Validator get amountValidator => AmountValidator(type: _wallet.type);
|
||||
Validator get amountValidator =>
|
||||
AmountValidator(currency: walletTypeToCryptoCurrency(_wallet.type));
|
||||
|
||||
Validator get allAmountValidator => AllAmountValidator();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user