From fc492a18c151886681e4b3ac65c7a477f43f0a45 Mon Sep 17 00:00:00 2001 From: samy Date: Tue, 17 Jun 2025 18:03:35 -1000 Subject: [PATCH] Fix multilple file input bug once and for all --- .../views/components/form/error.blade.php | 26 +++++-------------- .../views/components/form/input.blade.php | 15 ++++++++++- .../Form/Concerns/InteractsWithFormField.php | 16 +++++++++++- src/View/Components/Form/Input.php | 2 ++ 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/resources/views/components/form/error.blade.php b/resources/views/components/form/error.blade.php index 83071b6..aa3ffc4 100644 --- a/resources/views/components/form/error.blade.php +++ b/resources/views/components/form/error.blade.php @@ -1,21 +1,7 @@ -@props(['name', 'type' => 'text', 'multiple' => false]) +@props(['name']) -@if($type === 'file' && $multiple) - @foreach ($errors->get( rtrim($name, []) . '.*') as $messages) - @foreach ($messages as $message) -

- {{ $message }} -

- @endforeach - @endforeach -@else - @php - $message = $errors->first($name); - @endphp - - @if($message) -

- {{ $message }} -

- @endif -@endif \ No newline at end of file +@error($name) +

+ {{ $message }} +

+@enderror \ No newline at end of file diff --git a/resources/views/components/form/input.blade.php b/resources/views/components/form/input.blade.php index 6af963b..693fb8c 100644 --- a/resources/views/components/form/input.blade.php +++ b/resources/views/components/form/input.blade.php @@ -16,6 +16,7 @@ @if (! $isLivewire && ! empty($resolvedValue)) value="{{ $resolvedValue }}" @endif + @if($multiple) multiple @endif {{ $attributes->merge(['class' => $inputClasses]) }} /> @@ -24,5 +25,17 @@ {{ $help }} @endif - + + + @if($type === 'file' && $multiple) + @foreach($errors->get(rtrim($name, '[]') . '.*') as $error) +

+ {{ $error[0] }} +

+ + @endforeach + @else + + @endif + \ No newline at end of file diff --git a/src/View/Components/Form/Concerns/InteractsWithFormField.php b/src/View/Components/Form/Concerns/InteractsWithFormField.php index e3cee03..3d6126e 100644 --- a/src/View/Components/Form/Concerns/InteractsWithFormField.php +++ b/src/View/Components/Form/Concerns/InteractsWithFormField.php @@ -38,8 +38,22 @@ trait InteractsWithFormField public function render(): View { + $multipleInputFileHasError = false; - if (session('errors')?->has($this->name)) { + if(isset($this->type) && isset($this->multiple)) { + if($this->type === 'file' && $this->multiple) { + if(session('errors') !== null) + { + foreach (session('errors')->getMessages() as $key => $error) { + if (str_starts_with($key, rtrim($this->name, '[]') . '.')) { + $multipleInputFileHasError = true; + } + } + } + + } + } + if(session('errors')?->has($this->name) || $multipleInputFileHasError) { $this->inputClasses .= ' input-error'; } if($this->readonly) diff --git a/src/View/Components/Form/Input.php b/src/View/Components/Form/Input.php index c71fe04..36e30c7 100644 --- a/src/View/Components/Form/Input.php +++ b/src/View/Components/Form/Input.php @@ -19,6 +19,7 @@ class Input extends Component * @param bool $required * @param bool $disabled * @param bool $readonly Will act as disabled, but keep an HTML-sendable value + * @param bool $multiple If true, the input will be a multiple file upload input. * @param string $gridLayout The form section component offers a grid of 12 columns. Adjust this parameter as needed. * @param mixed|null $value * @param mixed|null $model If $model->$name exists, you can just set the model instead of a value. @@ -33,6 +34,7 @@ class Input extends Component public bool $required = false, public bool $disabled = false, public bool $readonly = false, + public bool $multiple = false, public string $gridLayout = '', public mixed $value = null, public mixed $model = null,