Beautiful Open-Source Form Builder
Ignore stale matrix rows (#1165)
Julien Nahum90ad312c0aa9
diff --git a/api/app/Http/Requests/AnswerFormRequest.php b/api/app/Http/Requests/AnswerFormRequest.phpindex 0f791215b..a822edaa9 100644--- a/api/app/Http/Requests/AnswerFormRequest.php+++ b/api/app/Http/Requests/AnswerFormRequest.php@@ -309,6 +309,13 @@ class AnswerFormRequest extends FormRequestif ($property['type'] === 'rich_text' && $receivedValue) {$mergeData[$property['id']] = Purify::clean($receivedValue);}++ if ($property['type'] === 'matrix' && is_array($receivedValue)) {+ $mergeData[$property['id']] = array_intersect_key(+ $receivedValue,+ array_flip($property['rows'] ?? [])+ );+ }});$this->merge($mergeData);diff --git a/api/app/Rules/MatrixValidationRule.php b/api/app/Rules/MatrixValidationRule.phpindex f50c1238b..4cc8b5891 100644--- a/api/app/Rules/MatrixValidationRule.php+++ b/api/app/Rules/MatrixValidationRule.php@@ -52,10 +52,5 @@ class MatrixValidationRule implements ValidationRule}}- // Check for extra rows that shouldn't be there- $extraRows = array_diff(array_keys($value), $rows);- foreach ($extraRows as $extraRow) {- $fail("Unexpected row '{$extraRow}' in the matrix.");- }}}diff --git a/api/tests/Feature/Forms/MatrixInputTest.php b/api/tests/Feature/Forms/MatrixInputTest.phpindex 8c08ea9bd..62dcdd9b9 100644--- a/api/tests/Feature/Forms/MatrixInputTest.php+++ b/api/tests/Feature/Forms/MatrixInputTest.php@@ -37,6 +37,48 @@ it('can submit form with valid matrix input', function () {]);});+it('silently ignores matrix rows that no longer exist', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace);++ $matrixProperty = [+ 'id' => 'matrix_field',+ 'name' => 'Matrix Question',+ 'type' => 'matrix',+ 'rows' => ['Row 2', 'Row 3'],+ 'columns' => ['Column A', 'Column B', 'Column C'],+ 'required' => true+ ];++ $form->properties = array_merge($form->properties, [$matrixProperty]);+ $form->update();++ $submissionData = [+ 'matrix_field' => [+ 'Row 1' => 'Column A',+ 'Row 2' => 'Column B',+ 'Row 3' => 'Column C'+ ]+ ];++ $formData = $this->generateFormSubmissionData($form, $submissionData);++ $this->postJson(route('forms.answer', $form->slug), $formData)+ ->assertSuccessful()+ ->assertJson([+ 'type' => 'success',+ 'message' => 'Form submission saved.',+ ]);++ $submission = $form->submissions()->latest()->first();++ expect($submission->data['matrix_field'])->toBe([+ 'Row 2' => 'Column B',+ 'Row 3' => 'Column C'+ ]);+});+it('cannot submit form with invalid matrix input', function () {$user = $this->actingAsUser();$workspace = $this->createUserWorkspace($user);