Skip to content

Commit

Permalink
Evaluate custom validation rules first
Browse files Browse the repository at this point in the history
  • Loading branch information
chiragchhatrala committed Dec 23, 2024
1 parent d18c4cc commit cc836e4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
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,
]);
});

0 comments on commit cc836e4

Please sign in to comment.