Conditioned field validation (#418)
* wip: validation condition input * form custom validation condition * Default message on form condition validation * field validation condition test * fix linting * update tests, add pass test * Polish UI --------- Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Forms\Form;
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
|
||||
it('can answer a form', function () {
|
||||
@@ -145,3 +146,94 @@ it('can not submit form with future dates', function () {
|
||||
'message' => 'The Date must be a date before tomorrow.',
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
it('can submit form with passed custom validation condition', function () {
|
||||
$user = $this->actingAsUser();
|
||||
$workspace = $this->createUserWorkspace($user);
|
||||
$form = $this->createForm($user, $workspace);
|
||||
$targetField = collect($form->properties)->where('name', 'Number')->first();
|
||||
$condition = [
|
||||
'actions' => [],
|
||||
'conditions' => [
|
||||
'operatorIdentifier' => 'or',
|
||||
'children' => [
|
||||
[
|
||||
'identifier' => $targetField['id'],
|
||||
'value' => [
|
||||
'operator' => 'greater_than',
|
||||
'property_meta' => [
|
||||
'id' => $targetField['id'],
|
||||
'type' => 'number',
|
||||
],
|
||||
'value' => 20,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$submissionData = [];
|
||||
$validationMessage = 'Number too low';
|
||||
$form->properties = collect($form->properties)->map(function ($property) use (&$submissionData, &$condition, &$validationMessage, $targetField) {
|
||||
if (in_array($property['name'], ['Name'])) {
|
||||
$property['validation'] = ['error_conditions' => $condition, 'error_message' => $validationMessage];
|
||||
$submissionData[$targetField['id']] = 100;
|
||||
}
|
||||
return $property;
|
||||
})->toArray();
|
||||
|
||||
$form->update();
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
|
||||
$response = $this->postJson(route('forms.answer', $form->slug), $formData);
|
||||
$response->assertSuccessful()
|
||||
->assertJson([
|
||||
'type' => 'success',
|
||||
'message' => 'Form submission saved.',
|
||||
]);
|
||||
});
|
||||
|
||||
it('can not submit form with failed custom validation condition', function () {
|
||||
$user = $this->actingAsUser();
|
||||
$workspace = $this->createUserWorkspace($user);
|
||||
$form = $this->createForm($user, $workspace);
|
||||
$targetField = collect($form->properties)->where('name', 'Email')->first();
|
||||
$condition = [
|
||||
'actions' => [],
|
||||
'conditions' => [
|
||||
'operatorIdentifier' => 'and',
|
||||
'children' => [
|
||||
[
|
||||
'identifier' => $targetField['id'],
|
||||
'value' => [
|
||||
'operator' => 'equals',
|
||||
'property_meta' => [
|
||||
'id' => $targetField['id'],
|
||||
'type' => 'email',
|
||||
],
|
||||
'value' => 'test@gmail.com',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$submissionData = [];
|
||||
$validationMessage = 'Can only use test@gmail.com';
|
||||
$form->properties = collect($form->properties)->map(function ($property) use (&$submissionData, &$condition, &$validationMessage, &$targetField) {
|
||||
if (in_array($property['name'], ['Name'])) {
|
||||
$property['validation'] = ['error_conditions' => $condition, 'error_message' => $validationMessage];
|
||||
$submissionData[$targetField['id']] = 'fail@gmail.com';
|
||||
}
|
||||
return $property;
|
||||
})->toArray();
|
||||
|
||||
$form->update();
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertStatus(422)
|
||||
->assertJson([
|
||||
'message' => $validationMessage,
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user