Problem/Motivation

We're using the currency input mask on a custom composite element that includes an 'adjustment' field that should allow negative amounts of dollars.

The JavaScript library providing functionality, Inputmask, has support for negative values and even recently fixed an edge-case bug with them.

Steps to reproduce

Put a currency input mask on a textfield, put a negative value in it, and try to submit the webform.

You will not be able to, and the message "Please match the requested format." will point to your field.

Proposed resolution

In `webform/src/Plugin/WebformElement/TextBase.php` change:

$input_masks = [                                                                                                                                           
    "'alias': 'currency'" => [
      'title' => $this->t('Currency'),
      'example' => '$ 9.99',
      'pattern' => '^\$ [0-9]{1,3}(,[0-9]{3})*.\d\d$',
    ],

to use the pattern: ^[-]?\$ [0-9]{1,3}(,[0-9]{3})*.\d\d$

Remaining tasks

Decide if we want this, have someone double-check the regex, and make a patch.

If not we can document using hook_webform_element_input_masks() to add a new input mask or hook_webform_element_input_masks_alter() to change the existing currency one (if we still refer to moduleHandler invoke and alter as hooks!).

User interface changes

None.

API changes

None?

Data model changes

None.

Comments

mlncn created an issue. See original summary.

jrockowitz’s picture

Version: 6.x-dev » 8.x-5.x-dev

I think changing the behavior of the currency input mask to support negative numbers would cause problems for people who don't want a negative currency value submitted.

We could add a "currency (-)" and "currency (+/-)" inputs mask aliases and change the title of the current currency mask alias to "Currency (+)".

Any changes should also be applied to Webform 8.x-5.x.

mlncn’s picture

Issue summary: View changes
mlncn’s picture

Status: Active » Needs review
StatusFileSize
new1.06 KB

Here's a patch giving us three total currency masks leaving the original unchanged except for the name— this applies cleanly against both 6.x and 8.x-5.x.

jrockowitz’s picture

My online feedback is to avoid abbreviations and change 'currency_pos_neg' to 'currency_positive_negative'.

mlncn’s picture

Updated patch without abbreviations— and two more places that need to account for the two new masks.

paulocs’s picture

StatusFileSize
new2.14 KB
new4.77 KB

Adding tests for the new feature.

marcusvsouza’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new272.76 KB

Patch applies properly and works fine.

jrockowitz’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new190.73 KB

There seems to be a bug where two dashes are being displayed when there is a default value. When the input has no default value, it works as expected. I tried upgrading the InputMask library, and that did not help.

I think we need to use the allowMinus: true option. When I tried using these options, a negative number was optional, and plus signs were not supported. We might need to only provide a negative number input mask.

@see https://stackoverflow.com/questions/40762041/jquery-inputmask-decimal-va...

Please make sure test changes via the web browser and not rely on the automated test.

paulocs’s picture

I'll work on it.

paulocs’s picture

Status: Needs work » Needs review
StatusFileSize
new521 bytes
new4.79 KB

New patch that fixes #9.

marcusvsouza’s picture

Status: Needs review » Needs work

The "-" do not appears when the negative mask is selected, and is not possible insert numbers without manually enter the negative signal.

marcusvsouza’s picture

The problem where it was not possible to enter numbers without manually entering the character "-" has been solved.

jrockowitz’s picture

Status: Needs work » Needs review
jrockowitz’s picture

StatusFileSize
new561 bytes
new4.78 KB

The only problem is a positive number can be submitted when only a negative input format is assigned.

Please review the attached patch and interdiff.

marcusvsouza’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new110.91 KB

Tested, worked as expected.

jrockowitz’s picture

Status: Reviewed & tested by the community » Fixed

  • jrockowitz authored c63c56c on 8.x-5.x
    Issue #3207724 by paulocs, mlncn, marcusvsouza, jrockowitz: Allow...

  • jrockowitz authored c63c56c on 6.x
    Issue #3207724 by paulocs, mlncn, marcusvsouza, jrockowitz: Allow...

Status: Fixed » Closed (fixed)

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