diff --git a/api/app/Http/Requests/AnswerFormRequest.php b/api/app/Http/Requests/AnswerFormRequest.php index 722f48fb..6431bbef 100644 --- a/api/app/Http/Requests/AnswerFormRequest.php +++ b/api/app/Http/Requests/AnswerFormRequest.php @@ -63,8 +63,14 @@ public function rules() $property['logic'] = false; }*/ - // For get values instead of Id for select/multi select options $data = $this->toArray(); + + // User custom validation + if (!(Str::of($property['type'])->startsWith('nf-')) && isset($property['validation'])) { + $rules[] = (new CustomFieldValidationRule($property['validation'], $data)); + } + + // For get values instead of Id for select/multi select options foreach ($selectionFields as $field) { if (isset($data[$field['id']]) && is_array($data[$field['id']])) { $data[$field['id']] = array_map(function ($val) use ($field) { @@ -109,11 +115,6 @@ public function rules() $rules = array_merge($rules, $this->getPropertyRules($property)); } - // User custom validation - if (!(Str::of($property['type'])->startsWith('nf-')) && isset($property['validation'])) { - $rules[] = (new CustomFieldValidationRule($property['validation'], $data)); - } - $this->requestRules[$propertyId] = $rules; } diff --git a/api/tests/Feature/Forms/AnswerFormTest.php b/api/tests/Feature/Forms/AnswerFormTest.php index b3bd8349..064ca601 100644 --- a/api/tests/Feature/Forms/AnswerFormTest.php +++ b/api/tests/Feature/Forms/AnswerFormTest.php @@ -303,3 +303,51 @@ $this->assertEquals(sizeof($errors), 1); $response->assertStatus(422); }); + + +it('executes custom validation before required field validation', function () { + $user = $this->actingAsUser(); + $workspace = $this->createUserWorkspace($user); + $form = $this->createForm($user, $workspace); + + $emailField = collect($form->properties)->where('name', 'Email')->first(); + $condition = [ + 'actions' => [], + 'conditions' => [ + 'operatorIdentifier' => 'and', + 'children' => [ + [ + 'identifier' => $emailField['id'], + 'value' => [ + 'operator' => 'contains', + 'property_meta' => [ + 'id' => $emailField['id'], + 'type' => 'email', + ], + 'value' => '@company.com', + ], + ], + ], + ], + ]; + + $submissionData = []; + $validationMessage = 'Must use company email'; + $form->properties = collect($form->properties)->map(function ($property) use (&$submissionData, &$condition, &$validationMessage, $emailField) { + if (in_array($property['name'], ['Name'])) { + $property['required'] = true; + $property['validation'] = ['error_conditions' => $condition, 'error_message' => $validationMessage]; + $submissionData[$emailField['id']] = 'test@other.com'; + } + return $property; + })->toArray(); + $form->update(); + + $formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData); + + $this->postJson(route('forms.answer', $form->slug), $formData) + ->assertStatus(422) + ->assertJson([ + 'message' => $validationMessage, + ]); +});