Fix: allow parent group field to have empty value

This commit is contained in:
philipp lang 2024-06-18 23:54:55 +02:00
parent af6dd35a5b
commit 8f6b8e466a
2 changed files with 17 additions and 1 deletions

View File

@ -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));
}

View File

@ -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();