63 lines
1.7 KiB
PHP
63 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class UpdateWebsiteRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
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', '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'],
|
|
];
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'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.',
|
|
];
|
|
}
|
|
}
|