Fix multilple file input bug once and for all

This commit is contained in:
samy
2025-06-17 18:03:35 -10:00
parent 56af3e1372
commit fc492a18c1
4 changed files with 37 additions and 22 deletions
@@ -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)
<p class="text-sm text-error mt-1">
@error($name)
<p class="text-sm text-error mt-1">
{{ $message }}
</p>
@endforeach
@endforeach
@else
@php
$message = $errors->first($name);
@endphp
@if($message)
<p class="text-sm text-error mt-1">
{{ $message }}
</p>
@endif
@endif
</p>
@enderror
@@ -16,6 +16,7 @@
@if (! $isLivewire && ! empty($resolvedValue))
value="{{ $resolvedValue }}"
@endif
@if($multiple) multiple @endif
{{ $attributes->merge(['class' => $inputClasses]) }}
/>
@@ -24,5 +25,17 @@
<x-sk::form.help>{{ $help }}</x-sk::form.help>
@endif
<x-sk::form.error :$name :$type @isset($multiple) :multiple="$multiple" @endisset />
@if($type === 'file' && $multiple)
@foreach($errors->get(rtrim($name, '[]') . '.*') as $error)
<p class="text-sm text-error mt-1">
{{ $error[0] }}
</p>
@endforeach
@else
<x-sk::form.error :name="$name" />
@endif
</div>
@@ -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)
+2
View File
@@ -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,