1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-15 03:05:26 +02:00
This commit is contained in:
Ryan Cramer
2022-07-08 12:09:18 -04:00
parent 2e76df1a89
commit 5d9ee59036

View File

@@ -72,16 +72,26 @@ class InputfieldFloat extends InputfieldInteger {
* *
*/ */
protected function sanitizeValue($value) { protected function sanitizeValue($value) {
if(!strlen("$value")) return ''; if(!strlen("$value")) {
if($this->digits > 0) { $value = '';
return is_numeric("$value") ? (string) $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)) { } else if(!is_float($value) && !is_int($value)) {
$value = $this->wire()->sanitizer->float($value, array('blankValue' => '')); $value = $this->wire()->sanitizer->float($value, array('blankValue' => ''));
if(!strlen("$value")) return ''; if(!strlen("$value")) $value = '';
} } else {
$precision = $this->precision; $precision = $this->precision;
if($precision === null || $precision === '') $precision = $this->getPrecision($value); if($precision === null || $precision === '') $precision = $this->getPrecision($value);
return is_int($precision) && $precision > 0 ? round((float) $value, $precision) : $value; $value = 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'])) { if($attributes && !empty($attributes['value']) && $this->noE && $this->hasE($attributes['value'])) {
$attributes['value'] = $this->wire()->sanitizer->float($attributes['value'], array('getString' => true)); $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); return parent::getAttributesString($attributes);
} }