Beautiful Open-Source Form Builder
Add validation conditions for form submissions based on existing entries (#991)
Chirag Chhatrala2dcb7d343f41
diff --git a/api/app/Service/Forms/FormLogicConditionChecker.php b/api/app/Service/Forms/FormLogicConditionChecker.phpindex bca76a203..1f3906e5a 100644--- a/api/app/Service/Forms/FormLogicConditionChecker.php+++ b/api/app/Service/Forms/FormLogicConditionChecker.php@@ -461,36 +461,52 @@ class FormLogicConditionCheckerreturn false;}- return FormSubmission::where('form_id', $formId)+ $fieldId = $condition['property_meta']['id'];++ // Validate field ID format to prevent SQL injection+ // Field IDs should only contain alphanumeric characters, underscores, and hyphens+ if (!preg_match('/^[a-zA-Z0-9_-]+$/', $fieldId)) {+ return false;+ }++ $dbConnection = config('database.default');++ // Use lockForUpdate to prevent race conditions when checking for uniqueness+ $query = FormSubmission::where('form_id', $formId)->where('status', '!=', FormSubmission::STATUS_PARTIAL)- ->where(function ($query) use ($condition, $fieldValue) {- $fieldId = $condition['property_meta']['id'];-- if (config('database.default') === 'mysql') {- // For scalar values- $query->where(function ($q) use ($fieldId, $fieldValue) {- $q->whereRaw("JSON_UNQUOTE(JSON_EXTRACT(data, '$.\"$fieldId\"')) = ?", [$fieldValue]);-- // For array values- if (is_array($fieldValue)) {- $q->orWhereRaw("JSON_CONTAINS(JSON_EXTRACT(data, '$.\"$fieldId\"'), ?)", [json_encode($fieldValue)]);- }- });- } else {- $query->where(function ($q) use ($fieldId, $fieldValue) {- // For scalar values- $q->whereRaw("data->? = ?::jsonb", [$fieldId, json_encode($fieldValue)]);-- // For array values- if (is_array($fieldValue)) {- $q->orWhereRaw("data->? @> ?::jsonb", [- $fieldId,- json_encode($fieldValue)- ]);- }- });- }- })->exists();+ ->lockForUpdate();++ if ($dbConnection === 'mysql') {+ // MySQL: Use fully parameterized JSON path query+ // JSON_EXTRACT with CONCAT for safe field ID handling+ if (is_array($fieldValue)) {+ // For array values (multi_select, matrix)+ $query->whereRaw(+ "JSON_CONTAINS(JSON_EXTRACT(data, CONCAT('\$.\"', ?, '\"')), ?)",+ [$fieldId, json_encode($fieldValue)]+ );+ } else {+ // For scalar values+ $query->whereRaw(+ "JSON_UNQUOTE(JSON_EXTRACT(data, CONCAT('\$.\"', ?, '\"'))) = ?",+ [$fieldId, $fieldValue]+ );+ }+ } else {+ // PostgreSQL: Already uses parameterized queries with -> operator+ if (is_array($fieldValue)) {+ // For array values (multi_select, matrix)+ $query->whereRaw("data->? @> ?::jsonb", [+ $fieldId,+ json_encode($fieldValue)+ ]);+ } else {+ // For scalar values+ $query->whereRaw("data->? = ?::jsonb", [$fieldId, json_encode($fieldValue)]);+ }+ }++ return $query->exists();}private function textConditionMet(array $propertyCondition, $value): bool@@ -618,6 +634,10 @@ class FormLogicConditionCheckerreturn $this->checkIsEmpty($propertyCondition, $value);case 'is_not_empty':return !$this->checkIsEmpty($propertyCondition, $value);+ case 'exists_in_submissions':+ return $this->checkExistsInSubmissions($propertyCondition, $value);+ case 'does_not_exist_in_submissions':+ return !$this->checkExistsInSubmissions($propertyCondition, $value);}return false;@@ -658,6 +678,10 @@ class FormLogicConditionCheckerreturn $this->checkNextMonth($propertyCondition, $value);case 'next_year':return $this->checkNextYear($propertyCondition, $value);+ case 'exists_in_submissions':+ return $this->checkExistsInSubmissions($propertyCondition, $value);+ case 'does_not_exist_in_submissions':+ return !$this->checkExistsInSubmissions($propertyCondition, $value);}return false;@@ -674,6 +698,10 @@ class FormLogicConditionCheckerreturn $this->checkIsEmpty($propertyCondition, $value);case 'is_not_empty':return !$this->checkIsEmpty($propertyCondition, $value);+ case 'exists_in_submissions':+ return $this->checkExistsInSubmissions($propertyCondition, $value);+ case 'does_not_exist_in_submissions':+ return !$this->checkExistsInSubmissions($propertyCondition, $value);}return false;@@ -702,6 +730,10 @@ class FormLogicConditionCheckerreturn $this->checkMatrixContains($propertyCondition, $value);case 'does_not_contain':return !$this->checkMatrixContains($propertyCondition, $value);+ case 'exists_in_submissions':+ return $this->checkExistsInSubmissions($propertyCondition, $value);+ case 'does_not_exist_in_submissions':+ return !$this->checkExistsInSubmissions($propertyCondition, $value);}return false;diff --git a/api/resources/data/open_filters.json b/api/resources/data/open_filters.jsonindex 02115e52d..9864c6a13 100644--- a/api/resources/data/open_filters.json+++ b/api/resources/data/open_filters.json@@ -368,6 +368,22 @@"format": {"type":"object"}+ },+ "exists_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true+ },+ "does_not_exist_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true}}},@@ -616,6 +632,22 @@},"content_length_less_than_or_equal_to": {"expected_type": "number"+ },+ "exists_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true+ },+ "does_not_exist_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true}}},@@ -650,6 +682,22 @@true]}+ },+ "exists_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true+ },+ "does_not_exist_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true}}},@@ -684,6 +732,22 @@true]}+ },+ "exists_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true+ },+ "does_not_exist_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true}}},@@ -814,6 +878,22 @@"type": "empty","values": "{}"}+ },+ "exists_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true+ },+ "does_not_exist_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true}}},@@ -906,6 +986,22 @@},"content_length_less_than_or_equal_to": {"expected_type": "number"+ },+ "exists_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true+ },+ "does_not_exist_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true}}},diff --git a/api/tests/Feature/Forms/FormValidationTest.php b/api/tests/Feature/Forms/FormValidationTest.phpnew file mode 100644index 000000000..7b311fb9e--- /dev/null+++ b/api/tests/Feature/Forms/FormValidationTest.php@@ -0,0 +1,490 @@+<?php++it('cannot submit form with duplicate value when does_not_exist_in_submissions validation is used for select field', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace, [+ 'properties' => [+ [+ 'id' => 'select_field',+ 'name' => 'Country',+ 'type' => 'select',+ 'hidden' => false,+ 'required' => false,+ 'select' => [+ 'options' => [+ ['id' => 'United States', 'name' => 'United States'],+ ['id' => 'United Kingdom', 'name' => 'United Kingdom'],+ ],+ ],+ 'validation' => [+ 'error_conditions' => [+ 'actions' => [],+ 'conditions' => [+ 'operatorIdentifier' => 'and',+ 'children' => [+ [+ 'identifier' => 'select_field',+ 'value' => [+ // Use does_not_exist to ensure uniqueness+ // Validation passes if value doesn't exist, fails if it does+ 'operator' => 'does_not_exist_in_submissions',+ 'property_meta' => [+ 'id' => 'select_field',+ 'type' => 'select',+ ],+ ],+ ],+ ],+ ],+ ],+ 'error_message' => 'This country has already been selected',+ ],+ ],+ [+ 'id' => 'title',+ 'name' => 'Name',+ 'type' => 'title',+ 'hidden' => false,+ 'required' => false+ ],+ ],+ ]);++ // First, create an existing submission with the same value+ $form->submissions()->create([+ 'data' => ['select_field' => 'United States'],+ ]);++ // Now try to submit the same value - should fail validation because value already exists+ $formData = [+ 'select_field' => 'United States'+ ];+ $this->postJson(route('forms.answer', $form->slug), $formData)+ ->assertStatus(422)+ ->assertJson([+ 'message' => 'This country has already been selected',+ ]);+});++it('cannot submit form with duplicate value when does_not_exist_in_submissions validation is used for date field', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace, [+ 'properties' => [+ [+ 'id' => 'date_field',+ 'name' => 'Event Date',+ 'type' => 'date',+ 'hidden' => false,+ 'required' => false,+ 'validation' => [+ 'error_conditions' => [+ 'actions' => [],+ 'conditions' => [+ 'operatorIdentifier' => 'and',+ 'children' => [+ [+ 'identifier' => 'date_field',+ 'value' => [+ 'operator' => 'does_not_exist_in_submissions',+ 'property_meta' => [+ 'id' => 'date_field',+ 'type' => 'date',+ ],+ ],+ ],+ ],+ ],+ ],+ 'error_message' => 'This date has already been used',+ ],+ ],+ [+ 'id' => 'title',+ 'name' => 'Name',+ 'type' => 'title',+ 'hidden' => false,+ 'required' => false+ ],+ ],+ ]);++ $testDate = now()->format('Y-m-d');++ // First, create an existing submission with the same date+ $form->submissions()->create([+ 'data' => ['date_field' => $testDate],+ ]);++ // Now try to submit the same date - should fail validation because date already exists+ $formData = ['date_field' => $testDate];+ $this->postJson(route('forms.answer', $form->slug), $formData)+ ->assertStatus(422)+ ->assertJson([+ 'message' => 'This date has already been used',+ ]);+});++it('cannot submit form with duplicate value when does_not_exist_in_submissions validation is used for multi_select field', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace, [+ 'properties' => [+ [+ 'id' => 'multi_select_field',+ 'name' => 'Interests',+ 'type' => 'multi_select',+ 'hidden' => false,+ 'required' => false,+ 'multi_select' => [+ 'options' => [+ ['id' => 'sports', 'name' => 'Sports'],+ ['id' => 'music', 'name' => 'Music'],+ ['id' => 'reading', 'name' => 'Reading'],+ ],+ ],+ 'validation' => [+ 'error_conditions' => [+ 'actions' => [],+ 'conditions' => [+ 'operatorIdentifier' => 'and',+ 'children' => [+ [+ 'identifier' => 'multi_select_field',+ 'value' => [+ 'operator' => 'does_not_exist_in_submissions',+ 'property_meta' => [+ 'id' => 'multi_select_field',+ 'type' => 'multi_select',+ ],+ ],+ ],+ ],+ ],+ ],+ 'error_message' => 'These interests have already been selected',+ ],+ ],+ [+ 'id' => 'title',+ 'name' => 'Name',+ 'type' => 'title',+ 'hidden' => false,+ 'required' => false+ ],+ ],+ ]);++ // First, create an existing submission with the same values+ $form->submissions()->create([+ 'data' => ['multi_select_field' => ['Sports', 'Music']],+ ]);++ // Now try to submit the same values - should fail validation because values already exist+ $formData = ['multi_select_field' => ['Sports', 'Music']];+ $this->postJson(route('forms.answer', $form->slug), $formData)+ ->assertStatus(422)+ ->assertJson([+ 'message' => 'These interests have already been selected',+ ]);+});++it('cannot submit form with duplicate value when does_not_exist_in_submissions validation is used for rating field', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace, [+ 'properties' => [+ [+ 'id' => 'rating_field',+ 'name' => 'Rating',+ 'type' => 'rating',+ 'hidden' => false,+ 'required' => false,+ 'validation' => [+ 'error_conditions' => [+ 'actions' => [],+ 'conditions' => [+ 'operatorIdentifier' => 'and',+ 'children' => [+ [+ 'identifier' => 'rating_field',+ 'value' => [+ 'operator' => 'does_not_exist_in_submissions',+ 'property_meta' => [+ 'id' => 'rating_field',+ 'type' => 'rating',+ ],+ ],+ ],+ ],+ ],+ ],+ 'error_message' => 'This rating value already exists',+ ]+ ],+ [+ 'id' => 'title',+ 'name' => 'Name',+ 'type' => 'title',+ 'hidden' => false,+ 'required' => false+ ],+ ],+ ]);++ // First, create an existing submission with the same rating+ $form->submissions()->create([+ 'data' => ['rating_field' => 5],+ ]);++ // Now try to submit the same rating - should fail validation because rating already exists+ $formData = ['rating_field' => 5];+ $this->postJson(route('forms.answer', $form->slug), $formData)+ ->assertStatus(422)+ ->assertJson([+ 'message' => 'This rating value already exists',+ ]);+});++it('cannot submit form with duplicate value when does_not_exist_in_submissions validation is used for scale field', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace, [+ 'properties' => [+ [+ 'id' => 'scale_field',+ 'name' => 'Satisfaction Scale',+ 'type' => 'scale',+ 'hidden' => false,+ 'required' => false,+ 'validation' => [+ 'error_conditions' => [+ 'actions' => [],+ 'conditions' => [+ 'operatorIdentifier' => 'and',+ 'children' => [+ [+ 'identifier' => 'scale_field',+ 'value' => [+ 'operator' => 'does_not_exist_in_submissions',+ 'property_meta' => [+ 'id' => 'scale_field',+ 'type' => 'scale',+ ],+ ],+ ],+ ],+ ],+ ],+ 'error_message' => 'This scale value already exists',+ ]+ ],+ [+ 'id' => 'title',+ 'name' => 'Name',+ 'type' => 'title',+ 'hidden' => false,+ 'required' => false+ ],+ ],+ ]);++ // First, create an existing submission with the same scale value+ $form->submissions()->create([+ 'data' => ['scale_field' => 7],+ ]);++ // Now try to submit the same scale value - should fail validation because it already exists+ $formData = ['scale_field' => 7];+ $this->postJson(route('forms.answer', $form->slug), $formData)+ ->assertStatus(422)+ ->assertJson([+ 'message' => 'This scale value already exists',+ ]);+});++it('cannot submit form with duplicate value when does_not_exist_in_submissions validation is used for slider field', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace, [+ 'properties' => [+ [+ 'id' => 'slider_field',+ 'name' => 'Slider Value',+ 'type' => 'slider',+ 'hidden' => false,+ 'required' => false,+ 'validation' => [+ 'error_conditions' => [+ 'actions' => [],+ 'conditions' => [+ 'operatorIdentifier' => 'and',+ 'children' => [+ [+ 'identifier' => 'slider_field',+ 'value' => [+ 'operator' => 'does_not_exist_in_submissions',+ 'property_meta' => [+ 'id' => 'slider_field',+ 'type' => 'slider',+ ],+ ],+ ],+ ],+ ],+ ],+ 'error_message' => 'This slider value already exists',+ ],+ ],+ [+ 'id' => 'title',+ 'name' => 'Name',+ 'type' => 'title',+ 'hidden' => false,+ 'required' => false+ ],+ ],+ ]);++ // First, create an existing submission with the same slider value+ $form->submissions()->create([+ 'data' => ['slider_field' => 50],+ ]);++ // Now try to submit the same slider value - should fail validation because it already exists+ $formData = ['slider_field' => 50];+ $this->postJson(route('forms.answer', $form->slug), $formData)+ ->assertStatus(422)+ ->assertJson([+ 'message' => 'This slider value already exists',+ ]);+});++it('can submit form with does_not_exist_in_submissions validation condition for select field', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace, [+ 'properties' => [+ [+ 'id' => 'select_field',+ 'name' => 'Country',+ 'type' => 'select',+ 'hidden' => false,+ 'required' => false,+ 'select' => [+ 'options' => [+ ['id' => 'United States', 'name' => 'United States'],+ ['id' => 'United Kingdom', 'name' => 'United Kingdom'],+ ],+ ],+ 'validation' => [+ 'error_conditions' => [+ 'actions' => [],+ 'conditions' => [+ 'operatorIdentifier' => 'and',+ 'children' => [+ [+ 'identifier' => 'select_field',+ 'value' => [+ 'operator' => 'does_not_exist_in_submissions',+ 'property_meta' => [+ 'id' => 'select_field',+ 'type' => 'select',+ ],+ ],+ ],+ ],+ ],+ ],+ 'error_message' => 'This country has already been selected',+ ],+ ],+ [+ 'id' => 'title',+ 'name' => 'Name',+ 'type' => 'title',+ 'hidden' => false,+ 'required' => false+ ],+ ],+ ]);++ $formData = ['select_field' => 'United States'];+ $this->postJson(route('forms.answer', $form->slug), $formData)+ ->assertSuccessful()+ ->assertJson([+ 'type' => 'success',+ 'message' => 'Form submission saved.',+ ]);+});++it('cannot submit form with duplicate value when does_not_exist_in_submissions validation is used for matrix field', function () {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace, [+ 'properties' => [+ [+ 'id' => 'matrix_field',+ 'name' => 'Matrix',+ 'type' => 'matrix',+ 'hidden' => false,+ 'required' => false,+ 'rows' => ['Row 1', 'Row 2'],+ 'columns' => ['Column 1', 'Column 2'],+ 'validation' => [+ 'error_conditions' => [+ 'actions' => [],+ 'conditions' => [+ 'operatorIdentifier' => 'and',+ 'children' => [+ [+ 'identifier' => 'matrix_field',+ 'value' => [+ 'operator' => 'does_not_exist_in_submissions',+ 'property_meta' => [+ 'id' => 'matrix_field',+ 'type' => 'matrix',+ ],+ ],+ ],+ ],+ ],+ ],+ 'error_message' => 'This matrix value already exists',+ ],+ ],+ [+ 'id' => 'title',+ 'name' => 'Name',+ 'type' => 'title',+ 'hidden' => false,+ 'required' => false+ ],+ ],+ ]);++ // First, create an existing submission with the same matrix values+ $form->submissions()->create([+ 'data' => [+ 'matrix_field' => [+ 'Row 1' => 'Column 1',+ 'Row 2' => 'Column 2',+ ]+ ],+ ]);++ // Now try to submit the same values - should fail validation because values already exist+ $submissionData = [+ 'matrix_field' => [+ 'Row 1' => 'Column 1',+ 'Row 2' => 'Column 2',+ ]+ ];+ $formData = $this->generateFormSubmissionData($form, $submissionData);+ $this->postJson(route('forms.answer', $form->slug), $formData)+ ->assertStatus(422)+ ->assertJson([+ 'message' => 'This matrix value already exists',+ ]);+});diff --git a/client/data/open_filters.json b/client/data/open_filters.jsonindex 02115e52d..9864c6a13 100644--- a/client/data/open_filters.json+++ b/client/data/open_filters.json@@ -368,6 +368,22 @@"format": {"type":"object"}+ },+ "exists_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true+ },+ "does_not_exist_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true}}},@@ -616,6 +632,22 @@},"content_length_less_than_or_equal_to": {"expected_type": "number"+ },+ "exists_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true+ },+ "does_not_exist_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true}}},@@ -650,6 +682,22 @@true]}+ },+ "exists_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true+ },+ "does_not_exist_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true}}},@@ -684,6 +732,22 @@true]}+ },+ "exists_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true+ },+ "does_not_exist_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true}}},@@ -814,6 +878,22 @@"type": "empty","values": "{}"}+ },+ "exists_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true+ },+ "does_not_exist_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true}}},@@ -906,6 +986,22 @@},"content_length_less_than_or_equal_to": {"expected_type": "number"+ },+ "exists_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true+ },+ "does_not_exist_in_submissions": {+ "expected_type": "boolean",+ "format": {+ "type": "enum",+ "values": [true]+ },+ "custom_validation_only": true}}},