some fixes

This commit is contained in:
Doar Mihai
2026-03-25 20:39:10 +02:00
parent 4db544bcc2
commit b5f948433b
8 changed files with 164 additions and 10 deletions
+34 -3
View File
@@ -11,18 +11,49 @@ class StoreWebsiteRequest extends FormRequest
return true;
}
protected function prepareForValidation(): void
{
// Normalize domain: remove protocol and www, trim slashes
if ($this->has('domain')) {
$domain = $this->input('domain');
// Remove protocol (http://, https://)
$domain = preg_replace('#^https?://#i', '', $domain);
// Remove www.
$domain = preg_replace('#^www\.#i', '', $domain);
// Remove trailing slashes and paths
$domain = strtok($domain, '/');
// Trim whitespace
$domain = trim($domain);
$this->merge([
'domain' => $domain,
]);
}
}
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'domain' => ['required', 'string', 'max:255', 'regex:/^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/'],
'name' => ['required', 'string', 'min:1', 'max:255'],
'domain' => [
'required',
'string',
'max:255',
'regex:/^([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/',
'unique:websites,domain,NULL,id,user_id,' . $this->user()->id,
],
];
}
public function messages(): array
{
return [
'domain.regex' => 'Please enter a valid domain (e.g. example.com).',
'domain.regex' => 'Please enter a valid domain (e.g. example.com or subdomain.example.com).',
'domain.unique' => 'You already have a website with this domain.',
];
}
}
+36 -3
View File
@@ -11,11 +11,43 @@ class UpdateWebsiteRequest extends FormRequest
return true;
}
protected function prepareForValidation(): void
{
// Normalize domain: remove protocol and www, trim slashes
if ($this->has('domain')) {
$domain = $this->input('domain');
// Remove protocol (http://, https://)
$domain = preg_replace('#^https?://#i', '', $domain);
// Remove www.
$domain = preg_replace('#^www\.#i', '', $domain);
// Remove trailing slashes and paths
$domain = strtok($domain, '/');
// Trim whitespace
$domain = trim($domain);
$this->merge([
'domain' => $domain,
]);
}
}
public function rules(): array
{
$websiteId = $this->route('website')->id;
return [
'name' => ['required', 'string', 'max:255'],
'domain' => ['required', 'string', 'max:255', 'regex:/^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/'],
'name' => ['required', 'string', 'min:1', 'max:255'],
'domain' => [
'required',
'string',
'max:255',
'regex:/^([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/',
'unique:websites,domain,' . $websiteId . ',id,user_id,' . $this->user()->id,
],
'is_active' => ['sometimes', 'boolean'],
];
}
@@ -23,7 +55,8 @@ class UpdateWebsiteRequest extends FormRequest
public function messages(): array
{
return [
'domain.regex' => 'Please enter a valid domain (e.g. example.com).',
'domain.regex' => 'Please enter a valid domain (e.g. example.com or subdomain.example.com).',
'domain.unique' => 'You already have a website with this domain.',
];
}
}
+24
View File
@@ -16,6 +16,7 @@ class Website extends Model
'user_id',
'name',
'domain',
'slug',
'tracking_id',
'is_active',
'settings',
@@ -35,9 +36,32 @@ class Website extends Model
if (empty($website->tracking_id)) {
$website->tracking_id = 'TA-' . strtoupper(Str::random(10));
}
// Auto-generate slug from name if not provided
if (empty($website->slug)) {
$baseSlug = Str::slug($website->name);
$slug = $baseSlug;
$counter = 1;
// Ensure slug uniqueness
while (static::where('slug', $slug)->exists()) {
$slug = $baseSlug . '-' . $counter;
$counter++;
}
$website->slug = $slug;
}
});
}
/**
* Get the route key for the model.
*/
public function getRouteKeyName(): string
{
return 'slug';
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);