From ddbe75a9883b46112d2a0d7b42e19b2950c828ef Mon Sep 17 00:00:00 2001 From: tienifr Date: Fri, 26 Jan 2024 18:26:08 +0700 Subject: [PATCH 1/2] fix: nvalid amount error displayed when value has more than 2 decimals --- .../ReimbursementAccount/ValidationStep.js | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/pages/ReimbursementAccount/ValidationStep.js b/src/pages/ReimbursementAccount/ValidationStep.js index 41f63b22f8f3..93230189b9d9 100644 --- a/src/pages/ReimbursementAccount/ValidationStep.js +++ b/src/pages/ReimbursementAccount/ValidationStep.js @@ -20,8 +20,11 @@ import TextLink from '@components/TextLink'; import withLocalize, {withLocalizePropTypes} from '@components/withLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import compose from '@libs/compose'; +import * as CurrencyUtils from '@libs/CurrencyUtils'; +import getPermittedDecimalSeparator from '@libs/getPermittedDecimalSeparator'; import BankAccount from '@libs/models/BankAccount'; import * as ValidationUtils from '@libs/ValidationUtils'; +import withPolicy from '@pages/workspace/withPolicy'; import WorkspaceResetBankAccountModal from '@pages/workspace/WorkspaceResetBankAccountModal'; import * as BankAccounts from '@userActions/BankAccounts'; import * as Report from '@userActions/Report'; @@ -61,23 +64,20 @@ const defaultProps = { * Any dollar amount (e.g. 1.12) will be returned as 112 * * @param {String} amount field input + * @param {RegExp} amountRegex * @returns {String} */ -const filterInput = (amount) => { +const filterInput = (amount, amountRegex?: RegExp) => { let value = amount ? amount.toString().trim() : ''; - if (value === '' || _.isNaN(Number(value)) || !Math.abs(Str.fromUSDToNumber(value))) { + value = value.replace(/^0+|0+$/g, ''); + if (value === '' || _.isNaN(Number(value)) || !Math.abs(Str.fromUSDToNumber(value)) || (amountRegex && !amountRegex.test(value))) { return ''; } - // If the user enters the values in dollars, convert it to the respective cents amount - if (_.contains(value, '.')) { - value = Str.fromUSDToNumber(value); - } - return value; }; -function ValidationStep({reimbursementAccount, translate, onBackButtonPress, account, policyID}) { +function ValidationStep({reimbursementAccount, translate, onBackButtonPress, account, policyID, toLocaleDigit, policy}) { const styles = useThemeStyles(); /** * @param {Object} values - form input values passed by the Form component @@ -85,9 +85,13 @@ function ValidationStep({reimbursementAccount, translate, onBackButtonPress, acc */ const validate = (values) => { const errors = {}; + const decimalSeparator = toLocaleDigit('.'); + const outputCurrency = lodashGet(policy, 'outputCurrency', CONST.CURRENCY.USD); + + const amountRegex = RegExp(String.raw`^-?\d{0,8}([${getPermittedDecimalSeparator(decimalSeparator)}]\d{0,${CurrencyUtils.getCurrencyDecimals(outputCurrency)}})?$`, 'i'); _.each(values, (value, key) => { - const filteredValue = typeof value === 'string' ? filterInput(value) : value; + const filteredValue = typeof value === 'string' ? filterInput(value, amountRegex) : value; if (ValidationUtils.isRequiredFulfilled(filteredValue)) { return; } @@ -231,6 +235,7 @@ ValidationStep.displayName = 'ValidationStep'; export default compose( withLocalize, + withPolicy, withOnyx({ account: { key: ONYXKEYS.ACCOUNT, From 8cf2f20d097b97a7a97b4eba1e351d98a94723a2 Mon Sep 17 00:00:00 2001 From: tienifr Date: Fri, 26 Jan 2024 18:35:28 +0700 Subject: [PATCH 2/2] fix remove type --- src/pages/ReimbursementAccount/ValidationStep.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/ReimbursementAccount/ValidationStep.js b/src/pages/ReimbursementAccount/ValidationStep.js index 93230189b9d9..2bebb4689440 100644 --- a/src/pages/ReimbursementAccount/ValidationStep.js +++ b/src/pages/ReimbursementAccount/ValidationStep.js @@ -67,7 +67,7 @@ const defaultProps = { * @param {RegExp} amountRegex * @returns {String} */ -const filterInput = (amount, amountRegex?: RegExp) => { +const filterInput = (amount, amountRegex) => { let value = amount ? amount.toString().trim() : ''; value = value.replace(/^0+|0+$/g, ''); if (value === '' || _.isNaN(Number(value)) || !Math.abs(Str.fromUSDToNumber(value)) || (amountRegex && !amountRegex.test(value))) {