Skip to content

Commit

Permalink
Merge pull request #35233 from tienifr/fix/34535
Browse files Browse the repository at this point in the history
fix: nvalid amount error displayed when value has more than 2 decimals
  • Loading branch information
amyevans authored Feb 6, 2024
2 parents 92db113 + 177087a commit 21dafcc
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/pages/ReimbursementAccount/ValidationStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -61,33 +64,34 @@ 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) => {
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
* @returns {Object}
*/
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;
}
Expand Down Expand Up @@ -231,6 +235,7 @@ ValidationStep.displayName = 'ValidationStep';

export default compose(
withLocalize,
withPolicy,
withOnyx({
account: {
key: ONYXKEYS.ACCOUNT,
Expand Down

0 comments on commit 21dafcc

Please sign in to comment.