diff --git a/app/Form/Fields/NamiField.php b/app/Form/Fields/NamiField.php index 38b875c8..244ca7c8 100644 --- a/app/Form/Fields/NamiField.php +++ b/app/Form/Fields/NamiField.php @@ -42,12 +42,13 @@ class NamiField extends Field public function getRegistrationRules(Form $form): array { $rules = [$this->key => 'present|array']; + $inputMembers = request($this->key); - foreach ($form->getFields()->forMembers()->noNamiField() as $field) { - $r = $field->getRegistrationRules($form); - - foreach ($r as $ruleKey => $rule) { - $rules[$this->key . '.*.' . $ruleKey] = $field->namiType === null ? $rule : ''; + foreach ($form->getFields()->noNamiField()->forMembers() as $field) { + foreach ($field->getRegistrationRules($form) as $ruleKey => $rule) { + foreach ($inputMembers as $memberIndex => $inputMember) { + $rules[$this->key . '.' . $memberIndex . '.' . $ruleKey] = !$inputMember['id'] || $field->namiType === null ? $rule : ''; + } } } @@ -69,12 +70,9 @@ class NamiField extends Field return []; } - $c = $form->getFields()->noNamiField()->forMembers(); - - foreach ($c as $field) { + foreach ($form->getFields()->noNamiField()->forMembers() as $field) { foreach ($field->getRegistrationRules($form) as $ruleKey => $rule) { foreach ($inputMembers as $memberIndex => $inputMember) { - $message = $field->name . ' für ein Mitglied'; $rules = array_merge( $rules, diff --git a/tests/Feature/Form/FormRegisterActionTest.php b/tests/Feature/Form/FormRegisterActionTest.php index 64322d4a..0d192b52 100644 --- a/tests/Feature/Form/FormRegisterActionTest.php +++ b/tests/Feature/Form/FormRegisterActionTest.php @@ -575,7 +575,6 @@ class FormRegisterActionTest extends FormTestCase public function testItAddsMemberForNonNami(): void { $this->login()->loginNami(); - $this->createMember(['mitgliedsnr' => '5505']); $form = Form::factory()->fields([ $this->namiField('members'), $this->textField('gender')->namiType(NamiType::GENDER)->required(false), @@ -589,6 +588,19 @@ class FormRegisterActionTest extends FormTestCase $this->assertEquals('Herr', $form->participants->get(1)->data['gender']); } + public function testItValidatesNamiTypeFieldsForNonMembers(): void + { + $this->login()->loginNami(); + $form = Form::factory()->fields([ + $this->namiField('members'), + $this->textField('gender')->name('Geschlecht')->namiType(NamiType::GENDER)->required(true), + ]) + ->create(); + + $this->register($form, ['gender' => 'Herr', 'members' => [['id' => null, 'gender' => null]]]) + ->assertJsonValidationErrors(['members.0.gender' => 'Geschlecht für ein Mitglied ist erforderlich.']); + } + public function testItValidatesMembersFields(): void { $this->login()->loginNami();