Add allowcustom field

This commit is contained in:
philipp lang 2024-04-16 23:15:05 +02:00
parent c62f8ec676
commit e480eb330e
6 changed files with 97 additions and 8 deletions

View File

@ -12,6 +12,7 @@ class DropdownField extends Field
public bool $required;
/** @var array<int, string> */
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
];
}

View File

@ -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<int, string> */
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
];
}

View File

@ -0,0 +1,58 @@
<?php
use App\Form\Models\Form;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
foreach (DB::table('forms')->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()
{
//
}
};

@ -1 +1 @@
Subproject commit ecf008f12130c8ba18fced50f0a6fe1378d88755
Subproject commit 765a9d009fc7fc33c0de17cb6e020083966e9a8f

View File

@ -28,6 +28,15 @@
inline
@update:modelValue="$emit('update:modelValue', {...modelValue, required: $event})"
></f-switch>
<f-switch
id="allowcustom"
:model-value="modelValue.allowcustom"
label="Eigene Option erlauben"
size="sm"
name="allowcustom"
inline
@update:modelValue="$emit('update:modelValue', {...modelValue, allowcustom: $event})"
></f-switch>
</template>
<script setup>

View File

@ -6,7 +6,6 @@ use App\Form\Enums\NamiType;
use App\Form\Enums\SpecialType;
use App\Form\Mails\ConfirmRegistrationMail;
use App\Form\Models\Form;
use App\Form\Models\Participant;
use App\Group;
use App\Group\Enums\Level;
use App\Member\Member;
@ -171,6 +170,12 @@ class FormRegisterActionTest extends FormTestCase
['letter.0' => 'Der gewählte Wert für Buchstabe ist ungültig.'],
];
yield [
$this->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->allowcustom(true),
['letter' => 'Z'],
null,
];
yield [
$this->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B']),
['letter' => 77],
@ -220,19 +225,19 @@ class FormRegisterActionTest extends FormTestCase
];
yield [
$this->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(false),
$this->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(false)->allowcustom(false),
['letter' => 'Z'],
['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.']
];
yield [
$this->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true),
$this->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true)->allowcustom(false),
['letter' => 'Z'],
['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.']
];
yield [
$this->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true),
$this->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true)->allowcustom(false),
['letter' => 'A'],
null
];