From 8f6b8e466a427bbec8303b6c8f2779071d895537 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Tue, 18 Jun 2024 23:54:55 +0200 Subject: [PATCH] Fix: allow parent group field to have empty value --- app/Form/Fields/GroupField.php | 2 +- tests/Feature/Form/FormRegisterActionTest.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/Form/Fields/GroupField.php b/app/Form/Fields/GroupField.php index d617faff..5e74af6b 100644 --- a/app/Form/Fields/GroupField.php +++ b/app/Form/Fields/GroupField.php @@ -65,7 +65,7 @@ class GroupField extends Field $rules[] = Rule::in(Group::find($this->parentGroup)->children()->pluck('id')->push(-1)); } - if ($this->parentField && request()->input($this->parentField)) { + if ($this->parentField && request()->input($this->parentField) && request()->input($this->parentField) !== -1) { $rules[] = Rule::in(Group::find(request()->input($this->parentField))->children()->pluck('id')->push(-1)); } diff --git a/tests/Feature/Form/FormRegisterActionTest.php b/tests/Feature/Form/FormRegisterActionTest.php index a2e5bdc6..64322d4a 100644 --- a/tests/Feature/Form/FormRegisterActionTest.php +++ b/tests/Feature/Form/FormRegisterActionTest.php @@ -344,6 +344,22 @@ class FormRegisterActionTest extends FormTestCase ->assertJsonValidationErrors(['group' => 'Der gewählte Wert für Gruppe ist ungültig.']); } + public function testGroupFieldCanBeUnsetWhenGiven(): void + { + $this->login()->loginNami(); + $group = Group::factory()->has(Group::factory(), 'children')->create(); + $form = Form::factory()->fields([ + $this->groupField('region')->emptyOptionValue('kein Bezirk')->parentGroup($group->id)->required(false), + $this->groupField('stamm')->name('Gruppe')->emptyOptionValue('kein Stamm')->parentField('region')->required(true) + ]) + ->create(); + + $this->register($form, ['region' => -1, 'stamm' => -1])->assertOk(); + $participants = $form->fresh()->participants; + $this->assertEquals(-1, $participants->first()->data['region']); + $this->assertEquals(-1, $participants->first()->data['stamm']); + } + public function testGroupFieldCanBeNullWhenNotRequired(): void { $this->login()->loginNami();