From 5d9ee59036eb8d3b694dd54f4f28e731a5d57112 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 8 Jul 2022 12:09:18 -0400 Subject: [PATCH] Attempt fix for processwire/processwire-issues#1594 --- .../modules/Inputfield/InputfieldFloat.module | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/wire/modules/Inputfield/InputfieldFloat.module b/wire/modules/Inputfield/InputfieldFloat.module index fbb71505..36dc485b 100644 --- a/wire/modules/Inputfield/InputfieldFloat.module +++ b/wire/modules/Inputfield/InputfieldFloat.module @@ -72,16 +72,26 @@ class InputfieldFloat extends InputfieldInteger { * */ protected function sanitizeValue($value) { - if(!strlen("$value")) return ''; - if($this->digits > 0) { - return is_numeric("$value") ? (string) $value : ''; + if(!strlen("$value")) { + $value = ''; + } else if($this->digits > 0) { + $value = (string) $value; + if(!is_numeric("$value")) { + $value = $this->wire()->sanitizer->float($value, array( + 'precision' => (int) $this->precision, + 'getString' => 'F', + 'blankValue' => '', + )); + } } else if(!is_float($value) && !is_int($value)) { $value = $this->wire()->sanitizer->float($value, array('blankValue' => '')); - if(!strlen("$value")) return ''; + if(!strlen("$value")) $value = ''; + } else { + $precision = $this->precision; + if($precision === null || $precision === '') $precision = $this->getPrecision($value); + $value = is_int($precision) && $precision > 0 ? round((float) $value, $precision) : $value; } - $precision = $this->precision; - if($precision === null || $precision === '') $precision = $this->getPrecision($value); - return is_int($precision) && $precision > 0 ? round((float) $value, $precision) : $value; + return $value; } /** @@ -138,6 +148,12 @@ class InputfieldFloat extends InputfieldInteger { if($attributes && !empty($attributes['value']) && $this->noE && $this->hasE($attributes['value'])) { $attributes['value'] = $this->wire()->sanitizer->float($attributes['value'], array('getString' => true)); } + if($attributes && $this->precision > 0 && $this->digits > 0) { + if(isset($attributes['value']) && strlen("$attributes[value]")) { + $f = $attributes['type'] === 'number' ? 'F' : 'f'; // F=non-locale aware, f=locale aware + $attributes['value'] = sprintf("%.{$this->precision}$f", (float) $attributes['value']); + } + } return parent::getAttributesString($attributes); }