Files
preregister/app/Http/Requests/Admin/ValidatesPreregistrationPageInput.php

94 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Admin;
use App\Models\PageBlock;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Validation\Rule;
trait ValidatesPreregistrationPageInput
{
/**
* @return array<string, array<int, ValidationRule|string>>
*/
protected function preregistrationPageSettingsRules(): array
{
return [
'title' => ['required', 'string', 'max:255'],
'thank_you_message' => ['nullable', 'string'],
'expired_message' => ['nullable', 'string'],
'ticketshop_url' => ['nullable', 'string', 'url:http,https', 'max:255'],
'post_submit_redirect_url' => ['nullable', 'string', 'url:http,https', 'max:500'],
'background_overlay_color' => ['nullable', 'string', 'regex:/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/'],
'background_overlay_opacity' => ['nullable', 'integer', 'min:0', 'max:100'],
'page_background' => ['nullable', 'file', 'image', 'mimes:jpeg,png,jpg,webp', 'max:5120'],
'remove_page_background' => ['sometimes', 'boolean'],
'start_date' => ['required', 'date'],
'end_date' => ['required', 'date', 'after:start_date'],
'is_active' => ['sometimes', 'boolean'],
];
}
/**
* @return array<string, array<int, ValidationRule|string>>
*/
protected function preregistrationPageBlocksRules(): array
{
return [
'blocks' => ['required', 'array', 'min:1'],
'blocks.*.type' => ['required', 'string', Rule::in(PageBlock::TYPES)],
'blocks.*.sort_order' => ['required', 'integer', 'min:0', 'max:9999'],
'blocks.*.is_visible' => ['sometimes', 'boolean'],
'blocks.*.content' => ['required', 'array'],
'blocks.*.remove_block_image' => ['sometimes', 'boolean'],
'blocks.*.block_image' => ['nullable', 'file', 'mimes:jpeg,png,jpg,webp,svg', 'max:5120'],
];
}
protected function preparePreregistrationPageSettings(): void
{
$ticketshop = $this->input('ticketshop_url');
$ticketshopNormalized = null;
if (is_string($ticketshop) && trim($ticketshop) !== '') {
$ticketshopNormalized = trim($ticketshop);
}
$redirect = $this->input('post_submit_redirect_url');
$redirectNormalized = null;
if (is_string($redirect) && trim($redirect) !== '') {
$redirectNormalized = trim($redirect);
}
$opacity = $this->input('background_overlay_opacity');
$opacityInt = null;
if ($opacity !== null && $opacity !== '') {
$opacityInt = max(0, min(100, (int) $opacity));
}
$this->merge([
'is_active' => $this->boolean('is_active'),
'remove_page_background' => $this->boolean('remove_page_background'),
'ticketshop_url' => $ticketshopNormalized,
'post_submit_redirect_url' => $redirectNormalized,
'background_overlay_opacity' => $opacityInt,
]);
}
/**
* @deprecated use preregistrationPageSettingsRules
*
* @return array<string, array<int, ValidationRule|string>>
*/
protected function preregistrationPageRules(): array
{
return array_merge($this->preregistrationPageSettingsRules(), $this->preregistrationPageBlocksRules());
}
protected function preparePreregistrationPageFields(): void
{
$this->preparePreregistrationPageSettings();
}
}