Skip to content

Commit cc738da

Browse files
committed
fix: always flash empty submissions with errors (closes #135)
1 parent 8cdd59f commit cc738da

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

classes/Models/SubmissionPage.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,19 @@ public function status(): string
545545
return $this->isFinished() ? 'listed' : 'unlisted';
546546
}
547547

548+
/**
549+
* Check if the submission has any filled values
550+
*/
551+
public function isEmpty(): bool
552+
{
553+
foreach ($this->values()->toArray() as $value) {
554+
if ($value !== null && $value !== '' && $value !== []) {
555+
return false;
556+
}
557+
}
558+
return true;
559+
}
560+
548561
/**
549562
* Return the corresponding form page
550563
*/

classes/Models/SubmissionSession.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,10 @@ private static function cleanupIfNeeded(SubmissionPage $submission): void
7676
// Only clean up if submission is finished
7777
// Don't clean up on validation errors - they're expected during form filling
7878
if ($submission->isFinished()) {
79-
$kirby = App::instance();
8079
$storage = $submission->storage();
8180

82-
if ($storage instanceof SubmissionSessionStorage) {
83-
$kirby->session()->remove(DreamForm::SESSION_KEY);
84-
$storage->cleanup();
85-
} elseif ($storage instanceof SubmissionCacheStorage) {
81+
if (method_exists($storage, 'cleanup')) {
82+
/** @var SubmissionSessionStorage|SubmissionCacheStorage $storage */
8683
$storage->cleanup();
8784
}
8885
}

config/hooks.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use tobimori\DreamForm\DreamForm;
44
use tobimori\DreamForm\Models\SubmissionPage;
5+
use tobimori\DreamForm\Storage\SubmissionSessionStorage;
56

67
return [
78
/**
@@ -21,6 +22,27 @@
2122
];
2223
},
2324

25+
/**
26+
* Clean up empty submissions with errors after rendering (flash behavior)
27+
*/
28+
'page.render:after' => function (string $contentType, array $data, string $html, Kirby\Cms\Page $page) {
29+
$submission = $data['submission'] ?? null;
30+
31+
// if submission exists, has errors, and is empty, clean it up after render
32+
if (
33+
$submission instanceof SubmissionPage &&
34+
!$submission->isSuccessful() &&
35+
$submission->isEmpty()
36+
) {
37+
$storage = $submission->storage();
38+
39+
if (method_exists($storage, 'cleanup')) {
40+
/** @var SubmissionSessionStorage $storage */
41+
$storage->cleanup();
42+
}
43+
}
44+
},
45+
2446
/*
2547
* Deletes all files associated with a submission page with elevated permissions,
2648
* so we can disallow deleting single files from the panel

0 commit comments

Comments
 (0)