Merge pull request #16627 from filamentphp/fix/double-file-upload-state-update-hooks

fix: Double file upload state update hooks
This commit is contained in:
Dan Harrin
2025-06-21 17:45:10 +01:00
committed by GitHub
2 changed files with 30 additions and 0 deletions
@@ -13,6 +13,7 @@ use Filament\Support\Contracts\TranslatableContentDriver;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Renderless;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
use Livewire\WithFileUploads;
@@ -28,6 +29,9 @@ trait InteractsWithForms
*/
public array $componentFileAttachments = [];
#[Locked]
public bool $areFormStateUpdateHooksDisabledForTesting = false;
/**
* @var array<string, Form>
*/
@@ -306,11 +310,33 @@ trait InteractsWithForms
public function updatedInteractsWithForms(string $statePath): void
{
if (app()->runningUnitTests() && $this->areFormStateUpdateHooksDisabledForTesting) {
return;
}
foreach ($this->getCachedForms() as $form) {
$form->callAfterStateUpdated($statePath);
}
}
public function disableFormStateUpdateHooksForTesting(): void
{
if (! app()->runningUnitTests()) {
return;
}
$this->areFormStateUpdateHooksDisabledForTesting = true;
}
public function enableFormStateUpdateHooksForTesting(): void
{
if (! app()->runningUnitTests()) {
return;
}
$this->areFormStateUpdateHooksDisabledForTesting = false;
}
protected function cacheForm(string $name, Form | Closure | null $form): ?Form
{
$this->isCachingForms = true;
@@ -45,6 +45,8 @@ class TestsForms
$state = Arr::undot([$formStatePath => $state]);
}
$this->call('disableFormStateUpdateHooksForTesting');
foreach (Arr::dot($state) as $key => $value) {
if ($value instanceof UploadedFile ||
(is_array($value) && isset($value[0]) && $value[0] instanceof UploadedFile)
@@ -54,6 +56,8 @@ class TestsForms
}
}
$this->call('enableFormStateUpdateHooksForTesting');
$this->call('fillFormDataForTesting', $state);
}