Skip to content

Commit 7ae686f

Browse files
Merge pull request #7663 from getkirby/forms/empty-value
refact: New ::emptyValue and ::fillWithEmptyValue methods for Field and FieldClass
2 parents 6f7d039 + 4b3e883 commit 7ae686f

32 files changed

+342
-5
lines changed

config/fields/checkboxes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
return $this->sanitizeOptions($this->value);
5151
},
5252
],
53+
'methods' => [
54+
'emptyValue' => function () {
55+
return [];
56+
}
57+
],
5358
'save' => function ($value): string {
5459
return A::join($value, ', ');
5560
},

config/fields/color.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@
106106
}
107107
],
108108
'methods' => [
109+
'emptyValue' => function () {
110+
return '';
111+
},
109112
'isColor' => function (string $value): bool {
110113
return
111114
$this->isHex($value) ||

config/fields/mixins/datetime.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
}
1313
],
1414
'methods' => [
15+
'emptyValue' => function () {
16+
return '';
17+
},
1518
'toDatetime' => function ($value, string $format = 'Y-m-d H:i:s') {
1619
if ($date = Date::optional($value)) {
1720
if ($this->step) {

config/fields/mixins/picker.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,9 @@
9090
return $text;
9191
},
9292
],
93+
'methods' => [
94+
'emptyValue' => function () {
95+
return [];
96+
}
97+
]
9398
];

config/fields/number.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
'step' => function ($step = null): float|string {
3131
return match ($step) {
3232
'any' => 'any',
33-
default => $this->toNumber($step) ?? ''
33+
default => $this->toNumber($step) ?? $this->emptyValue()
3434
};
3535
},
3636
'value' => function ($value = null) {
37-
return $this->toNumber($value) ?? '';
37+
return $this->toNumber($value) ?? $this->emptyValue();
3838
}
3939
],
4040
'computed' => [
@@ -45,10 +45,13 @@
4545
$default = $this->model()->toString($default);
4646
}
4747

48-
return $this->toNumber($default) ?? '';
48+
return $this->toNumber($default) ?? $this->emptyValue();
4949
}
5050
],
5151
'methods' => [
52+
'emptyValue' => function () {
53+
return '';
54+
},
5255
'toNumber' => function ($value): float|null {
5356
if ($this->isEmptyValue($value) === true) {
5457
return null;

config/fields/radio.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@
2626
'value' => function () {
2727
return $this->sanitizeOption($this->value) ?? '';
2828
}
29+
],
30+
'methods' => [
31+
'emptyValue' => function () {
32+
return '';
33+
}
2934
]
3035
];

config/fields/structure.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@
164164
}
165165
],
166166
'methods' => [
167+
'emptyValue' => function () {
168+
return [];
169+
},
167170
'rows' => function ($value) {
168171
$rows = Data::decode($value, 'yaml');
169172
$value = [];

config/fields/tags.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777
}
7878
],
7979
'methods' => [
80+
'emptyValue' => function () {
81+
return [];
82+
},
8083
'toValues' => function ($value) {
8184
if (is_null($value) === true) {
8285
return [];

config/fields/text.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@
103103
},
104104
];
105105
},
106+
'emptyValue' => function () {
107+
return '';
108+
}
106109
],
107110
'validations' => [
108111
'minlength',

config/fields/textarea.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@
113113
]
114114
];
115115
},
116+
'methods' => [
117+
'emptyValue' => function () {
118+
return '';
119+
}
120+
],
116121
'validations' => [
117122
'minlength',
118123
'maxlength'

0 commit comments

Comments
 (0)