Merge branch '4' into 5.1

This commit is contained in:
github-actions 2024-03-16 14:24:08 +00:00
commit c25922fedb
2 changed files with 22 additions and 33 deletions

View File

@ -101,10 +101,20 @@ class NumericField extends TextField
return NumberFormatter::TYPE_DOUBLE; return NumberFormatter::TYPE_DOUBLE;
} }
/**
* In some cases and locales, validation expects non-breaking spaces.
* This homogenises regular, narrow and thin non-breaking spaces to a regular space character.
*
*/
private function clean(?string $value): string
{
return trim(str_replace(["\u{00A0}", "\u{202F}", "\u{2009}"], ' ', $value ?? ''));
}
public function setSubmittedValue($value, $data = null) public function setSubmittedValue($value, $data = null)
{ {
// Save original value in case parse fails // Save original value in case parse fails
$value = trim($value ?? ''); $value = $this->clean($value);
$this->originalValue = $value; $this->originalValue = $value;
// Empty string is no-number (not 0) // Empty string is no-number (not 0)

View File

@ -11,27 +11,6 @@ class NumericFieldTest extends SapphireTest
{ {
protected $usesDatabase = false; protected $usesDatabase = false;
/**
* In some cases and locales, validation expects non-breaking spaces.
* This homogenises narrow and regular NBSPs to a regular space character
*
* Duplicates non-public NumericField::clean method
*
* @param string $input
* @return string The input value, with all non-breaking spaces replaced with spaces
*/
protected function clean($input)
{
return str_replace(
[
html_entity_decode(' ', 0, 'UTF-8'),
html_entity_decode(' ', 0, 'UTF-8'), // narrow non-breaking space
],
' ',
trim($input ?? '')
);
}
/** /**
* Test that data loaded in via Form::loadDataFrom(DataObject) will populate the field correctly, * Test that data loaded in via Form::loadDataFrom(DataObject) will populate the field correctly,
* and can format the database value appropriately for the frontend * and can format the database value appropriately for the frontend
@ -64,8 +43,8 @@ class NumericFieldTest extends SapphireTest
// Test expected formatted value // Test expected formatted value
$this->assertEquals( $this->assertEquals(
$this->clean($output), (string) $output,
$this->clean($field->Value()), $field->Value(),
"Expected $input to be formatted as $output in locale $locale" "Expected $input to be formatted as $output in locale $locale"
); );
@ -92,11 +71,11 @@ class NumericFieldTest extends SapphireTest
['nl_NL', null, '12.1', '12,1'], ['nl_NL', null, '12.1', '12,1'],
['nl_NL', 1, '14000.5', "14.000,5"], ['nl_NL', 1, '14000.5', "14.000,5"],
// fr // fr
['fr_FR', 0, '13000', "13 000"], ['fr_FR', 0, '13000', "13000"], // With a narrow non breaking space
['fr_FR', 0, '15', '15'], ['fr_FR', 0, '15', '15'],
['fr_FR', null, '12.0', '12,0'], ['fr_FR', null, '12.0', '12,0'],
['fr_FR', null, '12.1', '12,1'], ['fr_FR', null, '12.1', '12,1'],
['fr_FR', 1, '14000.5', "14 000,5"], ['fr_FR', 1, '14000.5', "14000,5"], // With a narrow non breaking space
// us // us
['en_US', 0, '13000', "13,000"], ['en_US', 0, '13000', "13,000"],
['en_US', 0, '15', '15'], ['en_US', 0, '15', '15'],
@ -172,15 +151,15 @@ class NumericFieldTest extends SapphireTest
['nl_NL', 1, '15,000.5', false], ['nl_NL', 1, '15,000.5', false],
// fr // fr
['fr_FR', 0, '13000', 13000, '13 000'], ['fr_FR', 0, '13000', 13000, '13000'], // With a narrow non breaking space
['fr_FR', 2, '12,00', 12.0], ['fr_FR', 2, '12,00', 12.0],
['fr_FR', 2, '12.00', false], ['fr_FR', 2, '12.00', false],
['fr_FR', 1, '11 000', 11000, '11 000,0'], ['fr_FR', 1, '11 000', 11000, '11000,0'], // With a narrow non breaking space
['fr_FR', 0, '11.000', 11000, '11 000'], ['fr_FR', 0, '11.000', 11000, '11000'], // With a narrow non breaking space
['fr_FR', null, '11,000', 11.000, '11,0'], ['fr_FR', null, '11,000', 11.000, '11,0'],
['fr_FR', 1, '15 000,5', 15000.5], ['fr_FR', 1, '15 000,5', 15000.5, '15000,5'], // With a narrow non breaking space
['fr_FR', 1, '15 000.5', false], ['fr_FR', 1, '15 000.5', false],
['fr_FR', 1, '15.000,5', 15000.5, '15 000,5'], ['fr_FR', 1, '15.000,5', 15000.5, '15000,5'], // With a narrow non breaking space
['fr_FR', 1, '15,000.5', false], ['fr_FR', 1, '15,000.5', false],
// us // us
['en_US', 0, '13000', 13000, '13,000'], ['en_US', 0, '13000', 13000, '13,000'],
@ -257,8 +236,8 @@ class NumericFieldTest extends SapphireTest
$cleanedInput = $submittedValue; $cleanedInput = $submittedValue;
} }
$this->assertEquals( $this->assertEquals(
$this->clean($cleanedInput), $cleanedInput,
$this->clean($field->Value()), $field->Value(),
"Expected input $submittedValue to be cleaned up as $cleanedInput in locale $locale" "Expected input $submittedValue to be cleaned up as $cleanedInput in locale $locale"
); );
} }