Commit e7716126 authored by bojanz's avatar bojanz

Issue #2840539 by skyredwang: Number validation initially fails for NULL values

parent 7de9b1d7
......@@ -67,12 +67,9 @@ class Number extends FormElement {
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input !== FALSE && $input !== NULL) {
if (!is_scalar($input)) {
$input = '0';
$input = '';
}
if ($input === '') {
$input = '0';
}
return $input;
return trim($input);
}
elseif (!empty($element['#default_value'])) {
// Convert the stored number to the local format. For example, "9.99"
......@@ -118,6 +115,9 @@ class Number extends FormElement {
*/
public static function validateNumber(array $element, FormStateInterface $form_state) {
$value = $form_state->getValue($element['#parents']);
if ($value === '') {
return;
}
$title = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
$number_formatter = self::getNumberFormatter($element);
......
......@@ -50,9 +50,9 @@ class NumberElementTest extends CommerceBrowserTestBase {
$this->submitForm($edit, 'Submit');
$this->assertSession()->pageTextContains('Amount must be lower than or equal to 100.');
// Valid submit.
// Valid submit. Ensure that the value is trimmed.
$edit = [
'number' => '10.99',
'number' => '10.99 ',
];
$this->submitForm($edit, 'Submit');
$this->assertSession()->pageTextContains('The number is "10.99".');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment