refactor: replace random ID generation with Cuid2 for unique HTML IDs in form components

This commit is contained in:
Andras Bacsai 2025-10-16 12:54:14 +02:00
parent a5c6f53b58
commit d2a334df78
5 changed files with 6 additions and 6 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;