Admin price fields multiply comma-decimal prices (e.g. EUR) by 100 on save when the admin JavaScript doesn't run
Admin price fields multiply comma-decimal prices (e.g. EUR) by 100 on save when the admin JavaScript doesn't run
Context
While editing products in the admin, the EUR price of a product kept growing on every save of the product form, even when only other fields (e.g. stock quantity) were changed: 10 → 1,000 → 100,000 → … USD and AUD prices were unaffected. It silently corrupted prices and made every save register as a price change.
In our case the admin JavaScript wasn't loading (an import-map pin was missing from our Docker image), but the underlying problem is that the price form only round-trips correctly when the money-field Stimulus controller runs.
Spree::Admin::FormBuilder#spree_money_field renders the amount using the currency's decimal mark (Money::Currency#decimal_mark), so an EUR amount is rendered as 1000,00. On submit, Spree::Price#amount= parses it with Spree::LocalizedNumber.parse, which uses the I18n locale's separator (. for en) and strips all other characters, so 1000,00 becomes 100000.
Expected Behavior
Saving the product form without changing a price leaves the price unchanged, whether or not the admin JavaScript has run. A value rendered by the server should parse back to the same amount.
Actual Behavior
If the money-field controller hasn't rewritten the value, the rendered 1000,00 is submitted as-is and stored as 100000.0. There's no validation error, and every save multiplies the price by 100. This affects any currency whose decimal mark differs from the admin locale's separator (e.g. EUR with an en admin).
Possible Fix
Make the server-rendered value use the same separator the server parses with, so a submit without JavaScript round-trips unchanged. For example, in spree_money_field, format with I18n.t('number.currency.format.separator') instead of the currency's decimal_mark, and let the Stimulus controller handle display formatting. Alternatively, Spree::Price#amount= could parse using the currency's decimal mark when the value was rendered that way, though that must not break API clients sending 1000.00.
Steps to Reproduce
- Use the admin with locale
enand a store that supports EUR. Create a product with an EUR price of10. - Open the product's edit page in the admin with JavaScript not running (e.g. disable JavaScript, or block the admin's
application-spree-adminmodule). The EUR amount field shows10,00. - Click Save without changing anything.
- Reopen the product: the EUR price is now
1000.00. Each further save multiplies it by 100 again.
Your Environment
- Version used: Spree 5.6.1 (spree_core, spree_api, spree_admin), Rails 8.1.3, Ruby 4.0.1, PostgreSQL 18, admin locale
en, store currencies USD, EUR, AUD - Gemfile and Gemfile.lock as text in a Gist:
- Any relevant stack traces ("Full trace" preferred): none, since the corruption is silent. The admin JavaScript failed to load in our setup with this browser console error, which is how the controller ended up not running:
Uncaught TypeError: Failed to resolve module specifier "@rails/actiontext". Relative references must start with either "/", "./", or "../".
Source: spree/spree