Skip to content

Commit 42572b8

Browse files
committed
Additional work for leading zeroes for number fields
1 parent 310494e commit 42572b8

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

classes/fields/number.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function schema( $options = null ) {
157157

158158
$schema = 'DECIMAL(' . $length . ',' . $decimals . ')';
159159

160-
if ( 1 === (int) pods_v( static::$type . '_keep_leading_zeroes', $options, 255 ) ) {
160+
if ( 1 === (int) pods_v( static::$type . '_keep_leading_zeroes', $options, 0 ) ) {
161161
$schema = 'VARCHAR(' . $length . ')';
162162
}
163163

@@ -361,13 +361,26 @@ public function pre_save( $value, $id = null, $name = null, $options = null, $fi
361361
return null;
362362
}
363363

364+
$prefix = null;
365+
366+
if (
367+
1 === (int) pods_v( static::$type . '_keep_leading_zeroes', $options, 0 )
368+
&& preg_match( '/^(0+)/', $value, $leading_zeroes )
369+
) {
370+
$prefix = $leading_zeroes[0];
371+
}
372+
364373
$value = number_format( (float) $value, $decimals, '.', '' );
365374

366375
// Optionally remove trailing decimal zero's.
367376
if ( pods_v( static::$type . '_format_soft', $options, false ) ) {
368377
$value = $this->trim_decimals( $value, '.' );
369378
}
370379

380+
if ( null !== $prefix ) {
381+
$value = $prefix . $value;
382+
}
383+
371384
return $value;
372385
}
373386

@@ -386,6 +399,15 @@ public function format( $value = null, $name = null, $options = null, $pod = nul
386399
$dot = $format_args['dot'];
387400
$decimals = $format_args['decimals'];
388401

402+
$prefix = null;
403+
404+
if (
405+
1 === (int) pods_v( static::$type . '_keep_leading_zeroes', $options, 0 )
406+
&& preg_match( '/^(0+)/', $value, $leading_zeroes )
407+
) {
408+
$prefix = $leading_zeroes[0];
409+
}
410+
389411
if ( 'i18n' === pods_v( static::$type . '_format', $options ) ) {
390412
$value = number_format_i18n( (float) $value, $decimals );
391413
} else {
@@ -397,6 +419,10 @@ public function format( $value = null, $name = null, $options = null, $pod = nul
397419
$value = $this->trim_decimals( $value, $dot );
398420
}
399421

422+
if ( null !== $prefix ) {
423+
$value = $prefix . $value;
424+
}
425+
400426
return $value;
401427
}
402428

ui/js/dfv/src/fields/number-field/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const NumberField = ( {
4444
// a formatted string, so be able to handle either one, but keep
4545
// a formatted version available locally.
4646
const [ formattedValue, setFormattedValue ] = useState(
47-
formatNumberWithPodsFormat( value, format, softFormat )
47+
formatNumberWithPodsFormat( value, format, softFormat, keepLeadingZeroes )
4848
);
4949

5050
useEffect( () => {

0 commit comments

Comments
 (0)