Skip to content

Commit 5a526f1

Browse files
committed
Adjust validation rule to accept negative amount
1 parent 0ebf2cd commit 5a526f1

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Rules/Money.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Money implements Rule
88
{
99
public const RULE_NAME = 'money';
1010

11-
private const MONEY_REGEX = '/^(([1-9]\d{0,2})(\d*|(\s\d{3})*)|0)(\.\d{1,4})?$/';
11+
private const MONEY_REGEX = '/^-?(([1-9]\d{0,2})(\d*|(\s\d{3})*)|0)(\.\d{1,4})?$/';
1212

1313
public function passes($attribute, $value): bool
1414
{

tests/Unit/Rules/MoneyTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,38 @@ public function inputsDataProvider(): array
3838
'input' => '0.2500',
3939
'result' => true,
4040
],
41+
[
42+
'input' => '-1500',
43+
'result' => true,
44+
],
45+
[
46+
'input' => '-1 500',
47+
'result' => true,
48+
],
49+
[
50+
'input' => '-1500.0',
51+
'result' => true,
52+
],
53+
[
54+
'input' => '-1 500.0',
55+
'result' => true,
56+
],
57+
[
58+
'input' => '-0',
59+
'result' => true,
60+
],
61+
[
62+
'input' => '-0.0',
63+
'result' => true,
64+
],
65+
[
66+
'input' => '-0.2500',
67+
'result' => true,
68+
],
69+
[
70+
'input' => '- 500.0',
71+
'result' => false,
72+
],
4173
[
4274
'input' => '0.25001',
4375
'result' => false,

0 commit comments

Comments
 (0)