2023-12-26 20:24:57 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Form\Actions;
|
|
|
|
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
use App\Form\Fields\Field;
|
|
|
|
use Illuminate\Validation\Validator;
|
|
|
|
use Lorisleiva\Actions\ActionRequest;
|
|
|
|
|
|
|
|
trait HasValidation
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
2023-12-27 22:54:58 +01:00
|
|
|
public function globalRules(): array
|
2023-12-26 20:24:57 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
'config' => 'array',
|
|
|
|
'config.sections.*.name' => 'required',
|
2024-06-18 14:54:44 +02:00
|
|
|
'config.sections.*.intro' => 'nullable|string',
|
2023-12-26 20:24:57 +01:00
|
|
|
'config.sections.*.fields' => 'array',
|
|
|
|
'config.sections.*.fields.*.name' => 'required|string',
|
|
|
|
'config.sections.*.fields.*.type' => ['required', 'string', Rule::in(array_column(Field::asMeta(), 'id'))],
|
|
|
|
'config.sections.*.fields.*.key' => ['required', 'string', 'regex:/^[a-zA-Z_]*$/'],
|
|
|
|
'config.sections.*.fields.*.columns' => 'required|array',
|
2024-02-03 17:51:27 +01:00
|
|
|
'config.sections.*.fields.*.*' => '',
|
2023-12-26 20:24:57 +01:00
|
|
|
'config.sections.*.fields.*.columns.mobile' => 'required|numeric|gt:0|lte:2',
|
|
|
|
'config.sections.*.fields.*.columns.tablet' => 'required|numeric|gt:0|lte:4',
|
|
|
|
'config.sections.*.fields.*.columns.desktop' => 'required|numeric|gt:0|lte:6',
|
2024-07-14 20:20:56 +02:00
|
|
|
'mail_top' => 'array',
|
|
|
|
'mail_bottom' => 'array',
|
2023-12-26 20:24:57 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
2023-12-31 21:46:52 +01:00
|
|
|
public function globalValidationAttributes(): array
|
2023-12-26 20:24:57 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'config.sections.*.name' => 'Sektionsname',
|
|
|
|
'config.sections.*.fields.*.name' => 'Feldname',
|
|
|
|
'config.sections.*.fields.*.type' => 'Feldtyp',
|
|
|
|
'config.sections.*.fields.*.key' => 'Feldkey',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withValidator(Validator $validator, ActionRequest $request): void
|
|
|
|
{
|
|
|
|
if (!$validator->passes()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($request->input('config.sections') as $sindex => $section) {
|
|
|
|
foreach (data_get($section, 'fields') as $findex => $field) {
|
|
|
|
$fieldClass = Field::classFromType($field['type']);
|
|
|
|
if (!$fieldClass) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($fieldClass::metaRules() as $fieldName => $rules) {
|
|
|
|
$validator->addRules(["config.sections.{$sindex}.fields.{$findex}.{$fieldName}" => $rules]);
|
|
|
|
}
|
|
|
|
foreach ($fieldClass::metaAttributes() as $fieldName => $attribute) {
|
|
|
|
$validator->addCustomAttributes(["config.sections.{$sindex}.fields.{$findex}.{$fieldName}" => $attribute]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|