45 lines
1.5 KiB
PHP
45 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Actions\Projects\DuplicateProjectConfiguration;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class StoreProjectTemplateRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return ($this->user()?->can('create-project') ?? false)
|
|
&& ($this->route('project') === null || $this->user()->can('admin-project', $this->route('project')));
|
|
}
|
|
|
|
protected function prepareForValidation(): void
|
|
{
|
|
if ($this->filled('slug')) {
|
|
$this->merge([
|
|
'slug' => Str::slug((string) $this->input('slug')),
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return array<string, ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$project = $this->route('project');
|
|
$baseTemplate = $project?->projectTemplate;
|
|
$publishingBase = $baseTemplate !== null && ! $baseTemplate->is_published;
|
|
|
|
return [
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'slug' => ['required', 'string', 'max:255', 'alpha_dash', Rule::unique('project_templates', 'slug')->ignore($publishingBase ? $baseTemplate->id : null), Rule::when($publishingBase, Rule::in([$baseTemplate?->slug]))],
|
|
'copy_areas' => ['present', 'array'],
|
|
'copy_areas.*' => ['string', Rule::in(DuplicateProjectConfiguration::acceptedCopyAreas())],
|
|
];
|
|
}
|
|
}
|