Skip to content

Commit 4b3e883

Browse files
Merge pull request #7666 from getkirby/forms/empty-value-nico
Forms/empty value nico
2 parents 04ae3de + a4aa19e commit 4b3e883

File tree

11 files changed

+105
-101
lines changed

11 files changed

+105
-101
lines changed

panel/package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Form/Field/BlocksField.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ public function blocksToValues(
7777
return $result;
7878
}
7979

80-
public function emptyValue(): mixed
81-
{
82-
return [];
83-
}
84-
8580
public function fields(string $type): array
8681
{
8782
return $this->fieldset($type)->fields();

src/Form/Field/EntriesField.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class EntriesField extends FieldClass
3131
protected array $field;
3232
protected Form $form;
3333
protected bool $sortable = true;
34+
protected mixed $value = [];
3435

3536
public function __construct(array $params = [])
3637
{
@@ -43,11 +44,6 @@ public function __construct(array $params = [])
4344
$this->setSortable($params['sortable'] ?? true);
4445
}
4546

46-
public function emptyValue(): mixed
47-
{
48-
return [];
49-
}
50-
5147
public function field(): array
5248
{
5349
return $this->field;

src/Form/Field/LayoutField.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ public function __construct(array $params)
3030
parent::__construct($params);
3131
}
3232

33-
public function emptyValue(): mixed
34-
{
35-
return [];
36-
}
37-
3833
/**
3934
* @psalm-suppress MethodSignatureMismatch
4035
* @todo Remove psalm suppress after https://github.com/vimeo/psalm/issues/8673 is fixed

src/Form/FieldClass.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ public function props(): array
161161
];
162162
}
163163

164+
/**
165+
* @since 5.2.0
166+
* @todo Move to `Value` mixin once array-based fields are unsupported
167+
*/
168+
public function reset(): static
169+
{
170+
$this->value = $this->emptyValue();
171+
return $this;
172+
}
173+
164174
protected function setDisabled(bool $disabled = false): void
165175
{
166176
$this->disabled = $disabled;

src/Form/Fields.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ public function reset(): static
268268

269269
// reset the values of each field
270270
foreach ($this->data as $field) {
271-
$field->fillWithEmptyValue();
271+
if ($field instanceof Field) {
272+
$field->fillWithEmptyValue();
273+
} else {
274+
$field->reset();
275+
}
272276
}
273277

274278
return $this;

src/Form/Mixin/Value.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Kirby\Form\Mixin;
44

55
use Kirby\Cms\Language;
6+
use ReflectionProperty;
67

78
/**
89
* @package Kirby Form
@@ -56,7 +57,7 @@ public function default(): mixed
5657
*/
5758
public function emptyValue(): mixed
5859
{
59-
return null;
60+
return (new ReflectionProperty($this, 'value'))->getDefaultValue();
6061
}
6162

6263
/**
@@ -68,18 +69,6 @@ public function fill(mixed $value): static
6869
return $this;
6970
}
7071

71-
/**
72-
* Preferred name would be `::reset` but this is
73-
* taken by options in other fields.
74-
*
75-
* @since 5.2.0
76-
*/
77-
public function fillWithEmptyValue(): static
78-
{
79-
$this->value = $this->emptyValue();
80-
return $this;
81-
}
82-
8372
/**
8473
* Checks if the field has a value
8574
*/

tests/Form/Field/BlocksFieldTest.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,6 @@ public function testDefaultProps(): void
2222
$this->assertTrue($field->save());
2323
}
2424

25-
public function testFillWithEmptyValue(): void
26-
{
27-
$field = $this->field('blocks');
28-
29-
$field->fill([
30-
[
31-
'type' => 'heading',
32-
'content' => [
33-
'text' => 'a'
34-
]
35-
],
36-
[
37-
'type' => 'heading',
38-
'content' => [
39-
'text' => 'b'
40-
]
41-
],
42-
]);
43-
44-
$this->assertCount(2, $field->toFormValue());
45-
46-
$field->fillWithEmptyValue();
47-
48-
$this->assertSame([], $field->toFormValue());
49-
}
50-
5125
public function testGroups(): void
5226
{
5327
$field = $this->field('blocks', [
@@ -229,6 +203,32 @@ public function testRequiredValid(): void
229203
$this->assertTrue($field->isValid());
230204
}
231205

206+
public function testReset(): void
207+
{
208+
$field = $this->field('blocks');
209+
210+
$field->fill([
211+
[
212+
'type' => 'heading',
213+
'content' => [
214+
'text' => 'a'
215+
]
216+
],
217+
[
218+
'type' => 'heading',
219+
'content' => [
220+
'text' => 'b'
221+
]
222+
],
223+
]);
224+
225+
$this->assertCount(2, $field->toFormValue());
226+
227+
$field->reset();
228+
229+
$this->assertSame([], $field->toFormValue());
230+
}
231+
232232
public function testRoutes(): void
233233
{
234234
$field = $this->field('blocks');

tests/Form/Field/EntriesFieldTest.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,25 +126,6 @@ public function testFieldCounter(): void
126126
$this->assertArrayNotHasKey('counter', $field->field());
127127
}
128128

129-
public function testFillWithEmptyValue(): void
130-
{
131-
$value = [
132-
'https://getkirby.com',
133-
'https://forum.getkirby.com',
134-
'https://plugins.getkirby.com',
135-
];
136-
137-
$field = $this->field('entries');
138-
139-
$field->fill($value);
140-
141-
$this->assertSame($value, $field->toFormValue());
142-
143-
$field->fillWithEmptyValue();
144-
145-
$this->assertSame([], $field->toFormValue());
146-
}
147-
148129
public function testDefaultValue(): void
149130
{
150131
$field = $this->field('entries', [
@@ -275,6 +256,25 @@ public function testRequiredInvalid(): void
275256
$this->assertSame(1, $field->min());
276257
}
277258

259+
public function testReset(): void
260+
{
261+
$value = [
262+
'https://getkirby.com',
263+
'https://forum.getkirby.com',
264+
'https://plugins.getkirby.com',
265+
];
266+
267+
$field = $this->field('entries');
268+
269+
$field->fill($value);
270+
271+
$this->assertSame($value, $field->toFormValue());
272+
273+
$field->reset();
274+
275+
$this->assertSame([], $field->toFormValue());
276+
}
277+
278278
public static function supportsProvider(): array
279279
{
280280
return [

tests/Form/Field/LayoutFieldTest.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,33 +71,6 @@ public function testExtends(): void
7171
$this->assertArrayHasKey('background-color', $fields);
7272
}
7373

74-
public function testFillWithEmptyValue(): void
75-
{
76-
$value = [
77-
[
78-
'columns' => [
79-
[
80-
'blocks' => [
81-
[
82-
'type' => 'heading',
83-
]
84-
]
85-
]
86-
]
87-
]
88-
];
89-
90-
$field = $this->field('layout');
91-
92-
$field->fill($value);
93-
94-
$this->assertCount(1, $field->toFormValue());
95-
96-
$field->fillWithEmptyValue();
97-
98-
$this->assertSame([], $field->toFormValue());
99-
}
100-
10174
public function testLayouts(): void
10275
{
10376
$field = $this->field('layout', [
@@ -146,6 +119,33 @@ public function testProps(): void
146119
$this->assertSame([['1/1']], $props['layouts']);
147120
}
148121

122+
public function testReset(): void
123+
{
124+
$value = [
125+
[
126+
'columns' => [
127+
[
128+
'blocks' => [
129+
[
130+
'type' => 'heading',
131+
]
132+
]
133+
]
134+
]
135+
]
136+
];
137+
138+
$field = $this->field('layout');
139+
140+
$field->fill($value);
141+
142+
$this->assertCount(1, $field->toFormValue());
143+
144+
$field->reset();
145+
146+
$this->assertSame([], $field->toFormValue());
147+
}
148+
149149
public function testRoutes(): void
150150
{
151151
$field = $this->field('layout');

0 commit comments

Comments
 (0)