From 6ae61655deee1afac21467ad3741883e7e3d58ad Mon Sep 17 00:00:00 2001 From: philipp lang Date: Mon, 10 Jun 2024 17:46:41 +0200 Subject: [PATCH] Add minmax for checkboxes --- app/Form/Fields/CheckboxesField.php | 18 +++++- .../2024_06_10_170213_add_minmax_field.php | 64 +++++++++++++++++++ .../js/views/formtemplate/CheckboxesField.vue | 18 ++++++ tests/Feature/Form/FormRegisterActionTest.php | 24 +++++++ 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2024_06_10_170213_add_minmax_field.php diff --git a/app/Form/Fields/CheckboxesField.php b/app/Form/Fields/CheckboxesField.php index 6934b2bf..1a9769f8 100644 --- a/app/Form/Fields/CheckboxesField.php +++ b/app/Form/Fields/CheckboxesField.php @@ -13,6 +13,8 @@ class CheckboxesField extends Field { /** @var array */ public array $options; + public ?int $min; + public ?int $max; public static function name(): string { @@ -23,6 +25,8 @@ class CheckboxesField extends Field { return [ ['key' => 'options', 'default' => [], 'rules' => ['options' => 'array', 'options.*' => 'required|string'], 'label' => 'Optionen'], + ['key' => 'min', 'default' => null, 'rules' => ['min' => 'present'], 'label' => 'minimale Anzahl'], + ['key' => 'max', 'default' => null, 'rules' => ['max' => 'present'], 'label' => 'maximale Anzahl'], ]; } @@ -35,6 +39,8 @@ class CheckboxesField extends Field { return [ 'options' => $faker->words(4), + 'min' => null, + 'max' => null, ]; } @@ -43,8 +49,18 @@ class CheckboxesField extends Field */ public function getRegistrationRules(Form $form): array { + $globalRules = ['array']; + + if ($this->min > 0) { + $globalRules[] = 'min:' . $this->min; + } + + if ($this->max > 0) { + $globalRules[] = 'max:' . $this->max; + } + return [ - $this->key => 'array', + $this->key => $globalRules, $this->key . '.*' => ['string', Rule::in($this->options)], ]; } diff --git a/database/migrations/2024_06_10_170213_add_minmax_field.php b/database/migrations/2024_06_10_170213_add_minmax_field.php new file mode 100644 index 00000000..3084f6cb --- /dev/null +++ b/database/migrations/2024_06_10_170213_add_minmax_field.php @@ -0,0 +1,64 @@ +get() as $event) { + $config = json_decode($event->config); + $config->sections = array_map(function ($section) { + /** @var Collection */ + $fields = $section->fields; + $section->fields = collect($fields)->map(function ($field) { + if ($field->type === 'CheckboxesField') { + $field->min = 0; + $field->max = 0; + } + + return $field; + })->all(); + + return $section; + }, $config->sections); + DB::table('forms')->where('id', $event->id)->update(['config' => json_encode($config)]); + } + + foreach (DB::table('formtemplates')->get() as $event) { + $config = json_decode($event->config); + $config->sections = array_map(function ($section) { + /** @var Collection */ + $fields = $section->fields; + $section->fields = collect($fields)->map(function ($field) { + if ($field->type === 'CheckboxesField') { + $field->min = 0; + $field->max = 0; + } + + return $field; + })->all(); + + return $section; + }, $config->sections); + DB::table('formtemplates')->where('id', $event->id)->update(['config' => json_encode($config)]); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +}; diff --git a/resources/js/views/formtemplate/CheckboxesField.vue b/resources/js/views/formtemplate/CheckboxesField.vue index dc311d76..cf1f9247 100644 --- a/resources/js/views/formtemplate/CheckboxesField.vue +++ b/resources/js/views/formtemplate/CheckboxesField.vue @@ -18,6 +18,24 @@ > Option einfügen + + diff --git a/tests/Feature/Form/FormRegisterActionTest.php b/tests/Feature/Form/FormRegisterActionTest.php index 6d16edd3..74b25c6e 100644 --- a/tests/Feature/Form/FormRegisterActionTest.php +++ b/tests/Feature/Form/FormRegisterActionTest.php @@ -200,6 +200,30 @@ class FormRegisterActionTest extends FormTestCase null, ]; + yield [ + $this->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(0)->max(2), + ['letter' => ['A', 'B', 'C']], + ['letter' => 'Buchstabe darf maximal 2 Elemente haben.'], + ]; + + yield [ + $this->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(2)->max(0), + ['letter' => ['A']], + ['letter' => 'Buchstabe muss mindestens 2 Elemente haben.'], + ]; + + yield [ + $this->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(1)->max(0), + ['letter' => []], + ['letter' => 'Buchstabe muss mindestens 1 Elemente haben.'], + ]; + + yield [ + $this->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(0)->max(1), + ['letter' => ['A', 'B']], + ['letter' => 'Buchstabe darf maximal 1 Elemente haben.'], + ]; + yield [ $this->checkboxField('data')->name('Datenschutz')->required(false), ['data' => 5],