Check money field bounds on the submitted amount and explain ignored input

GF
1d ago
+1876-183

Problem

Follow-up to #5938 (the calculator in the money field):

  • Bounds (#5944). The min/max check rounded the amount through a JavaScript number, while the form submits the typed digits rounded as text. On a 2-decimal field with min: 0.01, 0.0049999999999999999999 passed the check as 0.01 and was submitted as 0.00 when Enter was pressed before blur.
  • Silent drops. A letter or a second decimal point in one number was dropped without any feedback, and text that could not be read only said so on submit.
  • Parentheses. ( and ) were dropped while typing, so 2*(3+4) became the valid 2*3+4 (10) without a word.

Change

  • The bounds check uses the same rounding as the submit (fixedAmountText on the typed text), so a value that passes is the value that is sent.
  • A keystroke the field does not take shows a short hint ("Numbers, + − × ÷ and ( ) only") for 2.5 seconds; text that cannot be read shows "Not a valid amount". The hint is an aria-live region and stays readable in privacy mode, as it carries no amount.
  • Parentheses work: 2*(3+4) is 14, nested and signed groups too. While a group is still open the result closes it (2*(3+4 already shows = 14.00). Unbalanced, empty or juxtaposed groups stay invalid.
  • A paste reads the way typing reads the same text. Signs and brackets follow typing, so (200.00) pastes as 200.00, -(-25) as 25, and (0,125) as 0.125, since a leading 0 is never thousands grouping. In a whole-number currency such as JPY, (1,200) and (1500.00) paste as 1200 and 1500, as typed. Only text typing cannot produce is read as a statement amount, where parentheses mean a negative amount: $(200.00), (1,200.00) or ¥(1,200). There is one named exception. A single separator after a leading 1–9 and before exactly three digits (20,000 in USD, 1.234 in EUR) keeps the currency's grouping when pasted, so 20,000 from a statement is 20000, although typed it is 20. A pasted amount is rounded on its own digits, keeps the decimal point it was written with, and goes in at the caret when it is pasted next to an operator in an expression.
  • The hint always takes its own line under the amount, so neither the hint nor a long expression is cut off, even in a narrow column. It takes no space while empty.

Effects on other parts of the app

Only the shared money field (_money_field, money_field_controller, evaluate_amount_expression). What is submitted for a valid amount does not change; an amount outside the bounds is now caught before submit in the one case above. No model, migration or API changes.

Screenshots

Phone (390 px, touch) and desktop (1400 px), before (main) and after:

PhoneDesktop
Parentheses
Rejected letter
Unreadable text
Below the minimum, Enter before blur (#5944): refused, nothing sent

Validation

  • System test: Enter before blur with 0.0049999999999999999999 on a min: 0.01 field is rejected.
  • JS unit tests for parentheses, groups, hints and rounding (test/javascript/evaluate_amount_expression_test.mjs).
  • Rubocop, ERB lint, Biome and Brakeman clean.

CHAIN

Closes #5944

🤖 Generated with Claude Code


Summary by cubic

Money fields now validate the exact rounded amount they submit, preventing values below the minimum from passing validation. They also evaluate grouped expressions and explain rejected or unfinished input instead of silently dropping it.

  • Parentheses support nested and signed groups, with exact decimal arithmetic for terminating calculations.
  • Pasted amounts follow the field’s parsing and rounding rules; pastes within expressions insert at the caret.
  • A paste emits input while the field is being edited and change on blur, matching typing behavior.
  • Inline hints explain invalid input and currency-switch values that need to be re-entered.

Only the shared money field changes; no model, migration, or API updates are required.

Summary by CodeRabbit

  • New Features
    • Money fields support nested calculations in parentheses and preview incomplete expressions with missing closing parentheses.
    • Temporary hints explain unsupported characters, extra decimal separators, and invalid expressions. Hints are available in multiple languages.
    • On narrow screens, the expression preview appears below the amount when no hint is shown.
  • Bug Fixes
    • Improved minimum and maximum validation for calculated amounts, including precision handling.
    • Invalid expressions no longer show a preview, and rejected input preserves the existing preview.