diff --git a/app/View/Components/Forms/Checkbox.php b/app/View/Components/Forms/Checkbox.php index d96e385f7..a759164fb 100644 --- a/app/View/Components/Forms/Checkbox.php +++ b/app/View/Components/Forms/Checkbox.php @@ -53,14 +53,14 @@ public function render(): View|Closure|string { // Store original ID for wire:model binding (property name) $this->modelBinding = $this->id; - $this->htmlId = $this->id; - // Generate unique HTML ID by prefixing with Livewire component ID if available + // Generate unique HTML ID by adding random suffix + // This prevents duplicate IDs when multiple forms are on the same page if ($this->id) { - $livewireId = $this->attributes?->wire('id'); - if ($livewireId) { - $this->htmlId = $livewireId.'-'.$this->id; - } + $uniqueSuffix = substr(md5(uniqid((string) mt_rand(), true)), 0, 8); + $this->htmlId = $this->id.'-'.$uniqueSuffix; + } else { + $this->htmlId = $this->id; } return view('components.forms.checkbox'); diff --git a/app/View/Components/Forms/Datalist.php b/app/View/Components/Forms/Datalist.php index e5bbbfb5c..08a320f68 100644 --- a/app/View/Components/Forms/Datalist.php +++ b/app/View/Components/Forms/Datalist.php @@ -56,20 +56,22 @@ public function render(): View|Closure|string if (is_null($this->id)) { $this->id = new Cuid2; - $this->modelBinding = $this->id; + // Don't create wire:model binding for auto-generated IDs + $this->modelBinding = 'null'; } - // Generate unique HTML ID by prefixing with Livewire component ID + // Generate unique HTML ID by adding random suffix // This prevents duplicate IDs when multiple forms are on the same page - $livewireId = $this->attributes?->wire('id'); - if ($livewireId && $this->modelBinding) { - $this->htmlId = $livewireId.'-'.$this->modelBinding; + if ($this->modelBinding && $this->modelBinding !== 'null') { + // Use original ID with random suffix for uniqueness + $uniqueSuffix = substr(md5(uniqid((string) mt_rand(), true)), 0, 8); + $this->htmlId = $this->modelBinding.'-'.$uniqueSuffix; } else { - $this->htmlId = $this->modelBinding ?: $this->id; + $this->htmlId = (string) $this->id; } if (is_null($this->name)) { - $this->name = $this->modelBinding; + $this->name = $this->modelBinding !== 'null' ? $this->modelBinding : (string) $this->id; } return view('components.forms.datalist'); diff --git a/app/View/Components/Forms/Input.php b/app/View/Components/Forms/Input.php index 37c126c0e..9a0c87c0a 100644 --- a/app/View/Components/Forms/Input.php +++ b/app/View/Components/Forms/Input.php @@ -52,19 +52,22 @@ public function render(): View|Closure|string if (is_null($this->id)) { $this->id = new Cuid2; - $this->modelBinding = $this->id; + // Don't create wire:model binding for auto-generated IDs + $this->modelBinding = 'null'; } - // Generate unique HTML ID by prefixing with Livewire component ID + + // Generate unique HTML ID by adding random suffix // This prevents duplicate IDs when multiple forms are on the same page - $livewireId = $this->attributes?->wire('id'); - if ($livewireId && $this->modelBinding) { - $this->htmlId = $livewireId.'-'.$this->modelBinding; + if ($this->modelBinding && $this->modelBinding !== 'null') { + // Use original ID with random suffix for uniqueness + $uniqueSuffix = substr(md5(uniqid((string) mt_rand(), true)), 0, 8); + $this->htmlId = $this->modelBinding.'-'.$uniqueSuffix; } else { - $this->htmlId = $this->modelBinding ?: $this->id; + $this->htmlId = (string) $this->id; } if (is_null($this->name)) { - $this->name = $this->modelBinding; + $this->name = $this->modelBinding !== 'null' ? $this->modelBinding : (string) $this->id; } if ($this->type === 'password') { $this->defaultClass = $this->defaultClass.' pr-[2.8rem]'; diff --git a/app/View/Components/Forms/Select.php b/app/View/Components/Forms/Select.php index c0811b5bd..54d83ded7 100644 --- a/app/View/Components/Forms/Select.php +++ b/app/View/Components/Forms/Select.php @@ -49,20 +49,22 @@ public function render(): View|Closure|string if (is_null($this->id)) { $this->id = new Cuid2; - $this->modelBinding = $this->id; + // Don't create wire:model binding for auto-generated IDs + $this->modelBinding = 'null'; } - // Generate unique HTML ID by prefixing with Livewire component ID + // Generate unique HTML ID by adding random suffix // This prevents duplicate IDs when multiple forms are on the same page - $livewireId = $this->attributes?->wire('id'); - if ($livewireId && $this->modelBinding) { - $this->htmlId = $livewireId.'-'.$this->modelBinding; + if ($this->modelBinding && $this->modelBinding !== 'null') { + // Use original ID with random suffix for uniqueness + $uniqueSuffix = substr(md5(uniqid((string) mt_rand(), true)), 0, 8); + $this->htmlId = $this->modelBinding.'-'.$uniqueSuffix; } else { - $this->htmlId = $this->modelBinding ?: $this->id; + $this->htmlId = (string) $this->id; } if (is_null($this->name)) { - $this->name = $this->modelBinding; + $this->name = $this->modelBinding !== 'null' ? $this->modelBinding : (string) $this->id; } return view('components.forms.select'); diff --git a/app/View/Components/Forms/Textarea.php b/app/View/Components/Forms/Textarea.php index cad85e167..e962efc29 100644 --- a/app/View/Components/Forms/Textarea.php +++ b/app/View/Components/Forms/Textarea.php @@ -62,20 +62,22 @@ public function render(): View|Closure|string if (is_null($this->id)) { $this->id = new Cuid2; - $this->modelBinding = $this->id; + // Don't create wire:model binding for auto-generated IDs + $this->modelBinding = 'null'; } - // Generate unique HTML ID by prefixing with Livewire component ID + // Generate unique HTML ID by adding random suffix // This prevents duplicate IDs when multiple forms are on the same page - $livewireId = $this->attributes?->wire('id'); - if ($livewireId && $this->modelBinding) { - $this->htmlId = $livewireId.'-'.$this->modelBinding; + if ($this->modelBinding && $this->modelBinding !== 'null') { + // Use original ID with random suffix for uniqueness + $uniqueSuffix = substr(md5(uniqid((string) mt_rand(), true)), 0, 8); + $this->htmlId = $this->modelBinding.'-'.$uniqueSuffix; } else { - $this->htmlId = $this->modelBinding ?: $this->id; + $this->htmlId = (string) $this->id; } if (is_null($this->name)) { - $this->name = $this->modelBinding; + $this->name = $this->modelBinding !== 'null' ? $this->modelBinding : (string) $this->id; } // $this->label = Str::title($this->label);