From d2a334df781de1a8505d7ab0116b13a0df2eb8f0 Mon Sep 17 00:00:00 2001 From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Date: Thu, 16 Oct 2025 12:54:14 +0200 Subject: [PATCH] refactor: replace random ID generation with Cuid2 for unique HTML IDs in form components --- app/View/Components/Forms/Checkbox.php | 3 ++- app/View/Components/Forms/Datalist.php | 2 +- app/View/Components/Forms/Input.php | 3 +-- app/View/Components/Forms/Select.php | 2 +- app/View/Components/Forms/Textarea.php | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/View/Components/Forms/Checkbox.php b/app/View/Components/Forms/Checkbox.php index a759164fb..eb38d84af 100644 --- a/app/View/Components/Forms/Checkbox.php +++ b/app/View/Components/Forms/Checkbox.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\View\View; use Illuminate\Support\Facades\Gate; use Illuminate\View\Component; +use Visus\Cuid2\Cuid2; class Checkbox extends Component { @@ -57,7 +58,7 @@ public function render(): View|Closure|string // Generate unique HTML ID by adding random suffix // This prevents duplicate IDs when multiple forms are on the same page if ($this->id) { - $uniqueSuffix = substr(md5(uniqid((string) mt_rand(), true)), 0, 8); + $uniqueSuffix = new Cuid2; $this->htmlId = $this->id.'-'.$uniqueSuffix; } else { $this->htmlId = $this->id; diff --git a/app/View/Components/Forms/Datalist.php b/app/View/Components/Forms/Datalist.php index 08a320f68..3b7a9ee34 100644 --- a/app/View/Components/Forms/Datalist.php +++ b/app/View/Components/Forms/Datalist.php @@ -64,7 +64,7 @@ public function render(): View|Closure|string // This prevents duplicate IDs when multiple forms are on the same page if ($this->modelBinding && $this->modelBinding !== 'null') { // Use original ID with random suffix for uniqueness - $uniqueSuffix = substr(md5(uniqid((string) mt_rand(), true)), 0, 8); + $uniqueSuffix = new Cuid2; $this->htmlId = $this->modelBinding.'-'.$uniqueSuffix; } else { $this->htmlId = (string) $this->id; diff --git a/app/View/Components/Forms/Input.php b/app/View/Components/Forms/Input.php index 9a0c87c0a..5ed347f42 100644 --- a/app/View/Components/Forms/Input.php +++ b/app/View/Components/Forms/Input.php @@ -55,12 +55,11 @@ public function render(): View|Closure|string // Don't create wire:model binding for auto-generated IDs $this->modelBinding = 'null'; } - // Generate unique HTML ID by adding random suffix // This prevents duplicate IDs when multiple forms are on the same page if ($this->modelBinding && $this->modelBinding !== 'null') { // Use original ID with random suffix for uniqueness - $uniqueSuffix = substr(md5(uniqid((string) mt_rand(), true)), 0, 8); + $uniqueSuffix = new Cuid2; $this->htmlId = $this->modelBinding.'-'.$uniqueSuffix; } else { $this->htmlId = (string) $this->id; diff --git a/app/View/Components/Forms/Select.php b/app/View/Components/Forms/Select.php index 54d83ded7..026e3ba8c 100644 --- a/app/View/Components/Forms/Select.php +++ b/app/View/Components/Forms/Select.php @@ -57,7 +57,7 @@ public function render(): View|Closure|string // This prevents duplicate IDs when multiple forms are on the same page if ($this->modelBinding && $this->modelBinding !== 'null') { // Use original ID with random suffix for uniqueness - $uniqueSuffix = substr(md5(uniqid((string) mt_rand(), true)), 0, 8); + $uniqueSuffix = new Cuid2; $this->htmlId = $this->modelBinding.'-'.$uniqueSuffix; } else { $this->htmlId = (string) $this->id; diff --git a/app/View/Components/Forms/Textarea.php b/app/View/Components/Forms/Textarea.php index 9b91bc5a0..a5303b947 100644 --- a/app/View/Components/Forms/Textarea.php +++ b/app/View/Components/Forms/Textarea.php @@ -71,7 +71,7 @@ public function render(): View|Closure|string // This prevents duplicate IDs when multiple forms are on the same page if ($this->modelBinding && $this->modelBinding !== 'null') { // Use original ID with random suffix for uniqueness - $uniqueSuffix = substr(md5(uniqid((string) mt_rand(), true)), 0, 8); + $uniqueSuffix = new Cuid2; $this->htmlId = $this->modelBinding.'-'.$uniqueSuffix; } else { $this->htmlId = (string) $this->id;