Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate custom validation rules first #655

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions api/app/Http/Requests/AnswerFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
48 changes: 48 additions & 0 deletions api/tests/Feature/Forms/AnswerFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']] = '[email protected]';
}
return $property;
})->toArray();
$form->update();

$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);

$this->postJson(route('forms.answer', $form->slug), $formData)
->assertStatus(422)
->assertJson([
'message' => $validationMessage,
]);
});
Loading