Add a form element that accepts amounts of money and validates it.

Comments

xano’s picture

Status: Active » Needs review
StatusFileSize
new8.79 KB

The code is mostly based on Payment's payment_amount form element, with some minor changes, because Currency uses a different amount format, and some extra validation.

Before we can properly use this, we need locale-based amount display, though.

xano’s picture

Maybe we should also add a "currency_currency" element to select currencies, and a give the "currency_amount" element an option to retrieve the currency code to use from another form element.

xano’s picture

I'm thinking about moving validation to bartfeenstra/currency, because validating and parsing amounts from user input is complex business and I'd like to be able to get more feedback than just from within the Drupal community. I also haven't found any PHP internal function or 3rd party code that flexibly validates or parses such user input. Zend Currency only does calculations, conversions, and display, for instance.

The patch

  1. only validates negative amounts if they are prefixed with a minus sign
  2. does not allow grouping characters
  3. supports two types of decimal separators (periods/full stops and commas)

is_numeric() (which is used by Drupal Commerce as well)

  1. only validates negative amounts if they are prefixed with a minus sign
  2. does not allow grouping characters
  3. supports ony type of decimal separator (a period/full stop)

However

  1. Negative amounts can be wrapped in parentheses, or even be suffixed by minus signs
  2. Grouping separators are common
  3. I have found at least four different decimal separators
xano’s picture

The code I just committed to bartfeenstra/currency parses user input to floats

  1. and validates negative amounts that are surrounded by parentheses, prefixed or postfixed by minus signs, or any combination of these notations.
  2. but still doesn't allow grouping characters, because I have come to think this may be impossible to do without knowing which locale the amount is formatted in and I don't want the code to guess.
  3. and supports four types of decimal separators.
xano’s picture

StatusFileSize
new12.15 KB

Before the patch can be applied, a composer update needs to be performed and the patch needs to be rerolled to allow for recent changes in bartfeenstra/currency that were made to support this issue.

The patch adds a currency_amount form element that has three properties:
- currency code: if !== FALSE, then users can select the currency using a select element. Otherwise the currency is displayed.
- minimum amount: the amount entered by the user must not be less than this.
- maximum amount: the amount entered by the user must not be more than this.

Using CSS I tried to improve accessibility (hidden labels) and usability (currency and amount elements positioned next to each other).

amateescu’s picture

Hmm.. can't we include the lib updates through composer inside the patch?

xano’s picture

We could, but there are updates for more than one library. It wouldn't make sense to include all of them in the patch.

Status: Needs review » Needs work

The last submitted patch, currency_1847158_02.patch, failed testing.

amateescu’s picture

Status: Needs work » Needs review

#5: currency_1847158_02.patch queued for re-testing.

I ran a composer update --prefer-dist, was that the only thing holding back this patch?

Status: Needs review » Needs work

The last submitted patch, currency_1847158_02.patch, failed testing.

xano’s picture

Status: Needs work » Needs review
StatusFileSize
new10.64 KB

Re-roll.

amateescu’s picture

Status: Needs review » Fixed

Committed to 7.x-2.x.. finally :)

xano’s picture

Status: Fixed » Needs review
StatusFileSize
new2.44 KB

The original patch did not save the amount as a float to $form_state. This patch adds that functionality + test update.

amateescu’s picture

Status: Needs review » Fixed

Committed the followup to 7.x-2.x.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

xano’s picture

Assigned: xano » Unassigned