Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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