35 lines
1.4 KiB
PHP
35 lines
1.4 KiB
PHP
<?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 UpdateProjectRequest extends FormRequest
|
||
{
|
||
public function authorize(): bool
|
||
{
|
||
$project = $this->route('customerProject');
|
||
return $project instanceof Project && ($this->user()?->can('update', $project) ?? 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 {
|
||
$project = $this->route('customerProject');
|
||
$normalized = app(ProjectUrlNormalizer::class)->normalize((string) $this->input('website_url'));
|
||
if ($this->user()->projects()->where('domain', $normalized->domain)->whereKeyNot($project->id)->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());
|
||
}
|
||
});
|
||
}
|
||
}
|