Files
2026-08-29 15:53:53 +03:00

30 lines
1.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Http\Requests;
use App\Models\Project;
use App\Services\ProjectUrlNormalizer;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
use InvalidArgumentException;
class StoreProjectRequest extends FormRequest
{
public function authorize(): bool { return $this->user()?->can('create', Project::class) ?? false; }
public function rules(): array { return ['name' => ['required', 'string', 'max:150'], 'website_url' => ['required', 'string', 'max:2048']]; }
public function withValidator(Validator $validator): void
{
$validator->after(function (Validator $validator): void {
if ($validator->errors()->has('website_url')) return;
try {
$normalized = app(ProjectUrlNormalizer::class)->normalize((string) $this->input('website_url'));
if ($this->user()->projects()->where('domain', $normalized->domain)->exists()) {
$validator->errors()->add('website_url', 'Bu alan adı hesabınızda zaten kayıtlı.');
}
} catch (InvalidArgumentException $exception) {
$validator->errors()->add('website_url', $exception->getMessage());
}
});
}
}