From 111f087b8a96c4aa172bb8c5e4f6a18f865f511d Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Tue, 15 May 2018 08:56:09 -0400 Subject: [PATCH 1/3] Add $defaultValue, allow null values --- src/fields/Number.php | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/fields/Number.php b/src/fields/Number.php index 3b2946a2f21..7c44bf46824 100644 --- a/src/fields/Number.php +++ b/src/fields/Number.php @@ -37,6 +37,11 @@ public static function displayName(): string // Properties // ========================================================================= + /** + * @var int|float|null The default value for new elements + */ + public $defaultValue; + /** * @var int|float The minimum allowed number */ @@ -67,6 +72,11 @@ public function init() { parent::init(); + // Normalize $defaultValue + if ($this->defaultValue !== null && empty($this->defaultValue)) { + $this->defaultValue = null; + } + // Normalize $max if ($this->max !== null && empty($this->max)) { $this->max = null; @@ -134,7 +144,7 @@ public function normalizeValue($value, ElementInterface $element = null) $value = Localization::normalizeNumber($value['value'], $value['locale']); } - return $value; + return $this->isValueEmpty($value, $element) ? null : $value; } /** @@ -142,12 +152,18 @@ public function normalizeValue($value, ElementInterface $element = null) */ public function getInputHtml($value, ElementInterface $element = null): string { - $decimals = $this->decimals; + if ($this->isFresh($element) && $this->defaultValue !== null) { + $value = $this->defaultValue; + } + + if ($value !== null) { + $decimals = $this->decimals; - // If decimals is 0 (or null, empty for whatever reason), don't run this - if ($decimals) { - $decimalSeparator = Craft::$app->getLocale()->getNumberSymbol(Locale::SYMBOL_DECIMAL_SEPARATOR); - $value = number_format($value, $decimals, $decimalSeparator, ''); + // If decimals is 0 (or null, empty for whatever reason), don't run this + if ($decimals) { + $decimalSeparator = Craft::$app->getLocale()->getNumberSymbol(Locale::SYMBOL_DECIMAL_SEPARATOR); + $value = number_format($value, $decimals, $decimalSeparator, ''); + } } return ''. From f9457768b2a50b83b2f3cf37c9af6c58f87681b1 Mon Sep 17 00:00:00 2001 From: Brandon Kelly Date: Tue, 15 May 2018 07:09:13 -0700 Subject: [PATCH 2/3] Cleaner --- src/fields/Number.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/fields/Number.php b/src/fields/Number.php index 7c44bf46824..5dd2df670eb 100644 --- a/src/fields/Number.php +++ b/src/fields/Number.php @@ -156,14 +156,11 @@ public function getInputHtml($value, ElementInterface $element = null): string $value = $this->defaultValue; } - if ($value !== null) { - $decimals = $this->decimals; - - // If decimals is 0 (or null, empty for whatever reason), don't run this - if ($decimals) { - $decimalSeparator = Craft::$app->getLocale()->getNumberSymbol(Locale::SYMBOL_DECIMAL_SEPARATOR); - $value = number_format($value, $decimals, $decimalSeparator, ''); - } + + // If decimals is 0 (or null, empty for whatever reason), don't run this + if ($value !== null && $this->decimals) { + $decimalSeparator = Craft::$app->getLocale()->getNumberSymbol(Locale::SYMBOL_DECIMAL_SEPARATOR); + $value = number_format($value, $this->decimals, $decimalSeparator, ''); } return ''. From 746962a952de6be5bd05878cd9c9e294369b5278 Mon Sep 17 00:00:00 2001 From: Brandon Kelly Date: Tue, 15 May 2018 07:10:27 -0700 Subject: [PATCH 3/3] Let defaultValue be 0 --- src/fields/Number.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fields/Number.php b/src/fields/Number.php index 5dd2df670eb..91c90ba32cb 100644 --- a/src/fields/Number.php +++ b/src/fields/Number.php @@ -73,7 +73,7 @@ public function init() parent::init(); // Normalize $defaultValue - if ($this->defaultValue !== null && empty($this->defaultValue)) { + if ($this->defaultValue === '') { $this->defaultValue = null; }