Skip to content

Commit 6bf1ae9

Browse files
committed
Merge branch '5' into 6.0
2 parents b3f7b58 + 5101710 commit 6bf1ae9

File tree

2 files changed

+106
-14
lines changed

2 files changed

+106
-14
lines changed

src/Forms/NumericField.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,6 @@ protected function getNumberType()
119119
return NumberFormatter::TYPE_DOUBLE;
120120
}
121121

122-
/**
123-
* In some cases and locales, validation expects non-breaking spaces.
124-
* This homogenises regular, narrow and thin non-breaking spaces to a regular space character.
125-
*/
126-
private function clean(?string $value): string
127-
{
128-
return trim(str_replace(["\u{00A0}", "\u{202F}", "\u{2009}"], ' ', $value ?? ''));
129-
}
130-
131122
public function setValue($value, $data = null)
132123
{
133124
$this->originalValue = $value;
@@ -143,11 +134,10 @@ public function setSubmittedValue($value, $data = null)
143134
}
144135

145136
// Save original value in case parse fails
146-
$value = $this->clean($value);
147137
$this->originalValue = $value;
148138

149139
// Empty string is no-number (not 0)
150-
if (strlen($value ?? '') === 0) {
140+
if (mb_strlen($value ?? '') === 0) {
151141
$this->value = null;
152142
return $this;
153143
}
@@ -157,8 +147,8 @@ public function setSubmittedValue($value, $data = null)
157147
$parsed = 0;
158148
$value = $formatter->parse($value, $this->getNumberType(), $parsed); // Note: may store literal `false` for invalid values
159149
// Ensure that entire string is parsed
160-
if ($parsed < strlen($this->originalValue ?? '')) {
161-
$value = false;
150+
if ($parsed < mb_strlen($this->originalValue ?? '')) {
151+
$this->value = false;
162152
}
163153
$this->value = $this->cast($value);
164154
return $this;
@@ -218,7 +208,7 @@ protected function cast(mixed $value): mixed
218208
return false;
219209
}
220210
// If null or empty string, return null
221-
if (is_null($value) || is_string($value) && strlen($value) === 0) {
211+
if (is_null($value) || is_string($value) && mb_strlen($value) === 0) {
222212
return null;
223213
}
224214
// If non-numeric, then return as-is. This will be caught by the validation.

tests/php/Forms/NumericFieldTest.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,37 @@ public static function provideSetValue()
5151
'expDataValue' => '14000.5',
5252
'expValue' => '14.000,5',
5353
],
54+
// de_CH
55+
[
56+
'locale' => 'de_CH',
57+
'scale' => 0,
58+
'input' => '13000',
59+
'expValue' => "13’000"
60+
],
61+
[
62+
'locale' => 'de_CH',
63+
'scale' => 0,
64+
'input' => '15',
65+
'expValue' => '15'
66+
],
67+
[
68+
'locale' => 'de_CH',
69+
'scale' => null,
70+
'input' => '12.0',
71+
'expValue' => '12.0'
72+
],
73+
[
74+
'locale' => 'de_CH',
75+
'scale' => null,
76+
'input' => '12.1',
77+
'expValue' => '12.1'
78+
],
79+
[
80+
'locale' => 'de_CH',
81+
'scale' => 1,
82+
'input' => '14000.5',
83+
'expValue' => "14’000.5"
84+
],
5485
// nl
5586
[
5687
'locale' => 'nl_NL',
@@ -321,6 +352,74 @@ public static function dataForTestSubmittedValue()
321352
'submittedValue' => '15,000.5',
322353
'dataValue' => false,
323354
],
355+
// de_CH (Swiss German)
356+
[
357+
'locale' => 'de_CH',
358+
'scale' => 0,
359+
'submittedValue' => '13000',
360+
'dataValue' => 13000,
361+
'cleanedInput' => '13’000'
362+
],
363+
[
364+
'locale' => 'de_CH',
365+
'scale' => 2,
366+
'submittedValue' => '12,00',
367+
'dataValue' => false
368+
],
369+
[
370+
'locale' => 'de_CH',
371+
'scale' => 2,
372+
'submittedValue' => '12.00',
373+
'dataValue' => 12.00
374+
],
375+
[
376+
'locale' => 'de_CH',
377+
'scale' => 1,
378+
'submittedValue' => '11 000',
379+
'dataValue' => 11000,
380+
'cleanedInput' => "11’000.0"
381+
],
382+
[
383+
'locale' => 'de_CH',
384+
'scale' => 0,
385+
'submittedValue' => "11.000",
386+
'dataValue' => 11,
387+
'cleanedInput' => "11"
388+
],
389+
[
390+
'locale' => 'de_CH',
391+
'scale' => null,
392+
'submittedValue' => '11,000',
393+
'dataValue' => 11000.0,
394+
'cleanedInput' => '11’000.0'
395+
],
396+
[
397+
'locale' => 'de_CH',
398+
'scale' => 1,
399+
'submittedValue' => '15 000,5',
400+
'dataValue' => false
401+
],
402+
[
403+
'locale' => 'de_CH',
404+
'scale' => 1,
405+
'submittedValue' => "15 000.5",
406+
'dataValue' => 15000.5,
407+
'cleanedInput' => '15’000.5'
408+
],
409+
[
410+
'locale' => 'de_CH',
411+
'scale' => 1,
412+
'submittedValue' => '15.000,5',
413+
'dataValue' => false
414+
],
415+
[
416+
'locale' => 'de_CH',
417+
'scale' => 1,
418+
'submittedValue' => '15,000.5',
419+
'dataValue' => 15000.5,
420+
'cleanedInput' => '15’000.5'
421+
],
422+
// nl_nl (same as de)
324423
[
325424
'locale' => 'nl_NL',
326425
'scale' => 0,
@@ -385,6 +484,7 @@ public static function dataForTestSubmittedValue()
385484
'submittedValue' => '15,000.5',
386485
'dataValue' => false,
387486
],
487+
// fr
388488
[
389489
'locale' => 'fr_FR',
390490
'scale' => 0,
@@ -456,6 +556,7 @@ public static function dataForTestSubmittedValue()
456556
'submittedValue' => '15,000.5',
457557
'dataValue' => false,
458558
],
559+
// us
459560
[
460561
'locale' => 'en_US',
461562
'scale' => 0,
@@ -521,6 +622,7 @@ public static function dataForTestSubmittedValue()
521622
'submittedValue' => '15,000.5',
522623
'dataValue' => '15000.5',
523624
],
625+
// 'html5'
524626
[
525627
'locale' => 'html5',
526628
'scale' => 0,

0 commit comments

Comments
 (0)