From e480eb330e030bd94113f7e9dc757ed44357fc4d Mon Sep 17 00:00:00 2001 From: philipp lang Date: Tue, 16 Apr 2024 23:15:05 +0200 Subject: [PATCH] Add allowcustom field --- app/Form/Fields/DropdownField.php | 11 +++- app/Form/Fields/RadioField.php | 12 +++- ...024_04_16_233540_add_allowcustom_field.php | 58 +++++++++++++++++++ packages/adrema-form | 2 +- .../js/views/formtemplate/RadioField.vue | 9 +++ tests/Feature/Form/FormRegisterActionTest.php | 13 +++-- 6 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 database/migrations/2024_04_16_233540_add_allowcustom_field.php diff --git a/app/Form/Fields/DropdownField.php b/app/Form/Fields/DropdownField.php index 8e0ce922..42d13abc 100644 --- a/app/Form/Fields/DropdownField.php +++ b/app/Form/Fields/DropdownField.php @@ -12,6 +12,7 @@ class DropdownField extends Field public bool $required; /** @var array */ public array $options; + public bool $allowcustom; public static function name(): string { @@ -23,6 +24,7 @@ class DropdownField extends Field return [ ['key' => 'options', 'default' => [], 'rules' => ['options' => 'present|array', 'options.*' => 'string'], 'label' => 'Optionen'], ['key' => 'required', 'default' => true, 'rules' => ['required' => 'present|boolean'], 'label' => 'Erforderlich'], + ['key' => 'allowcustom', 'default' => false, 'rules' => ['required' => 'present|boolean'], 'label' => 'Eigene Option erlauben'], ]; } @@ -36,6 +38,7 @@ class DropdownField extends Field return [ 'options' => $faker->words(4), 'required' => $faker->boolean(), + 'allowcustom' => $faker->boolean(), ]; } @@ -44,8 +47,14 @@ class DropdownField extends Field */ public function getRegistrationRules(Form $form): array { + $rules = $this->required ? ['required', 'string'] : ['nullable', 'string']; + + if (!$this->allowcustom) { + $rules[] = Rule::in($this->options); + } + return [ - $this->key => $this->required ? ['required', 'string', Rule::in($this->options)] : ['nullable', 'string', Rule::in($this->options)], + $this->key => $rules ]; } diff --git a/app/Form/Fields/RadioField.php b/app/Form/Fields/RadioField.php index b19de8e8..ba7748e2 100644 --- a/app/Form/Fields/RadioField.php +++ b/app/Form/Fields/RadioField.php @@ -4,7 +4,6 @@ namespace App\Form\Fields; use App\Form\Models\Form; use App\Form\Models\Participant; -use App\Form\Presenters\Presenter; use Faker\Generator; use Illuminate\Validation\Rule; @@ -13,6 +12,7 @@ class RadioField extends Field public bool $required; /** @var array */ public array $options; + public bool $allowcustom; public static function name(): string { @@ -24,6 +24,7 @@ class RadioField extends Field return [ ['key' => 'options', 'default' => [], 'rules' => ['options' => 'present|array', 'options.*' => 'required|string'], 'label' => 'Optionen'], ['key' => 'required', 'default' => true, 'rules' => ['required' => 'present|boolean'], 'label' => 'Erforderlich'], + ['key' => 'allowcustom', 'default' => false, 'rules' => ['required' => 'present|boolean'], 'label' => 'Eigene Option erlauben'], ]; } @@ -37,6 +38,7 @@ class RadioField extends Field return [ 'options' => $faker->words(4), 'required' => $faker->boolean(), + 'allowcustom' => $faker->boolean(), ]; } @@ -45,8 +47,14 @@ class RadioField extends Field */ public function getRegistrationRules(Form $form): array { + $rules = $this->required ? ['required', 'string'] : ['nullable', 'string']; + + if (!$this->allowcustom) { + $rules[] = Rule::in($this->options); + } + return [ - $this->key => $this->required ? ['required', 'string', Rule::in($this->options)] : ['nullable', 'string', Rule::in($this->options)], + $this->key => $rules ]; } diff --git a/database/migrations/2024_04_16_233540_add_allowcustom_field.php b/database/migrations/2024_04_16_233540_add_allowcustom_field.php new file mode 100644 index 00000000..8482422f --- /dev/null +++ b/database/migrations/2024_04_16_233540_add_allowcustom_field.php @@ -0,0 +1,58 @@ +get() as $event) { + $config = json_decode($event->config); + $config->sections = array_map(function ($section) { + $section->fields = collect($section->fields)->map(function ($field) { + if ($field->type === 'DropdownField' || $field->type === 'RadioField') { + $field->allowcustom = false; + } + 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) { + $section->fields = collect($section->fields)->map(function ($field) { + if ($field->type === 'DropdownField' || $field->type === 'RadioField') { + $field->allowcustom = false; + } + 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/packages/adrema-form b/packages/adrema-form index ecf008f1..765a9d00 160000 --- a/packages/adrema-form +++ b/packages/adrema-form @@ -1 +1 @@ -Subproject commit ecf008f12130c8ba18fced50f0a6fe1378d88755 +Subproject commit 765a9d009fc7fc33c0de17cb6e020083966e9a8f diff --git a/resources/js/views/formtemplate/RadioField.vue b/resources/js/views/formtemplate/RadioField.vue index 98f52719..d4bb001f 100644 --- a/resources/js/views/formtemplate/RadioField.vue +++ b/resources/js/views/formtemplate/RadioField.vue @@ -28,6 +28,15 @@ inline @update:modelValue="$emit('update:modelValue', {...modelValue, required: $event})" > +