From 6262ccdd287b98c9c0d474f5cc5453810298c623 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Tue, 26 Dec 2023 20:24:57 +0100 Subject: [PATCH] Add validation for update --- app/Form/Actions/FormtemplateStoreAction.php | 59 +---------------- app/Form/Actions/FormtemplateUpdateAction.php | 1 + app/Form/Actions/HasValidation.php | 66 +++++++++++++++++++ app/Form/Fields/RadioField.php | 2 +- 4 files changed, 69 insertions(+), 59 deletions(-) create mode 100644 app/Form/Actions/HasValidation.php diff --git a/app/Form/Actions/FormtemplateStoreAction.php b/app/Form/Actions/FormtemplateStoreAction.php index 6ae0e528..30f5a048 100644 --- a/app/Form/Actions/FormtemplateStoreAction.php +++ b/app/Form/Actions/FormtemplateStoreAction.php @@ -2,51 +2,16 @@ namespace App\Form\Actions; -use App\Form\Fields\Field; use App\Form\Models\Formtemplate; use App\Lib\Events\Succeeded; use Illuminate\Http\JsonResponse; -use Illuminate\Validation\Rule; -use Illuminate\Validation\Validator; use Lorisleiva\Actions\ActionRequest; use Lorisleiva\Actions\Concerns\AsAction; class FormtemplateStoreAction { use AsAction; - - /** - * @return array - */ - public function rules(): array - { - return [ - 'name' => 'required|string|max:255', - 'config' => 'array', - 'config.sections.*.name' => 'required', - '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', - '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', - ]; - } - - /** - * @return array - */ - public function getValidationAttributes(): array - { - return [ - 'config.sections.*.name' => 'Sektionsname', - 'config.sections.*.fields.*.name' => 'Feldname', - 'config.sections.*.fields.*.type' => 'Feldtyp', - 'config.sections.*.fields.*.key' => 'Feldkey', - ]; - } + use HasValidation; /** * @param array $attributes @@ -63,26 +28,4 @@ class FormtemplateStoreAction Succeeded::message('Vorlage gespeichert.')->dispatch(); return response()->json([]); } - - 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]); - } - } - } - } } diff --git a/app/Form/Actions/FormtemplateUpdateAction.php b/app/Form/Actions/FormtemplateUpdateAction.php index dc252b70..77cc1615 100644 --- a/app/Form/Actions/FormtemplateUpdateAction.php +++ b/app/Form/Actions/FormtemplateUpdateAction.php @@ -10,6 +10,7 @@ use Lorisleiva\Actions\Concerns\AsAction; class FormtemplateUpdateAction { use AsAction; + use HasValidation; /** * @return array diff --git a/app/Form/Actions/HasValidation.php b/app/Form/Actions/HasValidation.php new file mode 100644 index 00000000..c3dad148 --- /dev/null +++ b/app/Form/Actions/HasValidation.php @@ -0,0 +1,66 @@ + + */ + public function rules(): array + { + return [ + 'name' => 'required|string|max:255', + 'config' => 'array', + 'config.sections.*.name' => 'required', + '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', + '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', + ]; + } + + /** + * @return array + */ + public function getValidationAttributes(): array + { + 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]); + } + } + } + } +} diff --git a/app/Form/Fields/RadioField.php b/app/Form/Fields/RadioField.php index 6c8a4165..13af8971 100644 --- a/app/Form/Fields/RadioField.php +++ b/app/Form/Fields/RadioField.php @@ -14,7 +14,7 @@ class RadioField extends Field public static function meta(): array { return [ - ['key' => 'options', 'default' => [], 'rules' => ['options' => 'present|array', 'options.*' => 'string'], 'label' => 'Optionen'], + ['key' => 'options', 'default' => [], 'rules' => ['options' => 'present|array', 'options.*' => 'required|string'], 'label' => 'Optionen'], ['key' => 'required', 'default' => false, 'rules' => ['required' => 'present|boolean'], 'label' => 'Erforderlich'], ]; }