Skip to content

Commit 10d7ae4

Browse files
committed
Additional work for leading zeroes for number fields
1 parent 75b52c3 commit 10d7ae4

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

@@ -358,13 +358,26 @@ public function pre_save( $value, $id = null, $name = null, $options = null, $fi
358358
return null;
359359
}
360360

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

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

377+
if ( null !== $prefix ) {
378+
$value = $prefix . $value;
379+
}
380+
368381
return $value;
369382
}
370383

@@ -383,6 +396,15 @@ public function format( $value = null, $name = null, $options = null, $pod = nul
383396
$dot = $format_args['dot'];
384397
$decimals = $format_args['decimals'];
385398

399+
$prefix = null;
400+
401+
if (
402+
1 === (int) pods_v( static::$type . '_keep_leading_zeroes', $options, 0 )
403+
&& preg_match( '/^(0+)/', $value, $leading_zeroes )
404+
) {
405+
$prefix = $leading_zeroes[0];
406+
}
407+
386408
if ( 'i18n' === pods_v( static::$type . '_format', $options ) ) {
387409
$value = number_format_i18n( (float) $value, $decimals );
388410
} else {
@@ -394,6 +416,10 @@ public function format( $value = null, $name = null, $options = null, $pod = nul
394416
$value = $this->trim_decimals( $value, $dot );
395417
}
396418

419+
if ( null !== $prefix ) {
420+
$value = $prefix . $value;
421+
}
422+
397423
return $value;
398424
}
399425

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)