Skip to content

Beautiful Open-Source Form Builder

Hidden field can never be required. Skip that validation (#716)

Chirag Chhatrala7b942997cecb
diff --git a/api/app/Http/Requests/AnswerFormRequest.php b/api/app/Http/Requests/AnswerFormRequest.php
index 6431bbef5..cb19bb70d 100644
--- a/api/app/Http/Requests/AnswerFormRequest.php
+++ b/api/app/Http/Requests/AnswerFormRequest.php
@@ -87,7 +87,10 @@ class AnswerFormRequest extends FormRequest
$data[$field['id']] = isset($tmpop['name']) ? $tmpop['name'] : $data[$field['id']];
}
}
- if (FormLogicPropertyResolver::isRequired($property, $data)) {
+ if (
+ FormLogicPropertyResolver::isRequired($property, $data) &&
+ !FormLogicPropertyResolver::isHidden($property, $data)
+ ) {
$rules[] = 'required';
if ($property['type'] == 'checkbox') {
diff --git a/api/tests/Feature/Forms/FormLogicTest.php b/api/tests/Feature/Forms/FormLogicTest.php
index e87560b72..77a5e6298 100644
--- a/api/tests/Feature/Forms/FormLogicTest.php
+++ b/api/tests/Feature/Forms/FormLogicTest.php
@@ -543,3 +543,67 @@ it('handles invalid regex patterns gracefully', function () {
'message' => $validationMessage,
]);
});
+
+it('skips validation for fields hidden by logic conditions', function () {
+ $user = $this->actingAsUser();
+ $workspace = $this->createUserWorkspace($user);
+ $form = $this->createForm($user, $workspace, [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'name' => 'Name',
+ 'type' => 'title',
+ 'hidden' => false,
+ 'required' => true,
+ 'logic' => [
+ 'conditions' => [
+ 'operatorIdentifier' => 'and',
+ 'children' => [
+ [
+ 'identifier' => 'email',
+ 'value' => [
+ 'operator' => 'is_empty',
+ 'property_meta' => [
+ 'id' => 'email_field',
+ 'type' => 'email',
+ ],
+ 'value' => true,
+ ],
+ ],
+ ],
+ ],
+ 'actions' => ['hide-block'],
+ ],
+ ],
+ [
+ 'id' => 'email_field',
+ 'name' => 'Email',
+ 'type' => 'email',
+ 'hidden' => false,
+ 'required' => false,
+ ],
+ ],
+ ]);
+
+ // Test when field should be hidden (email is empty)
+ $formData = ['email_field' => '']; // Empty email
+ $this->postJson(route('forms.answer', $form->slug), $formData)
+ ->assertSuccessful()
+ ->assertJson([
+ 'type' => 'success',
+ 'message' => 'Form submission saved.',
+ ]);
+
+ // Test when field should be visible (email is not empty)
+ $formData = ['email_field' => '[email protected]'];
+ $this->postJson(route('forms.answer', $form->slug), $formData)
+ ->assertStatus(422)
+ ->assertJson([
+ 'message' => 'The Name field is required.',
+ 'errors' => [
+ 'title' => [
+ 'The Name field is required.',
+ ],
+ ],
+ ]);
+});
diff --git a/client/components/open/forms/components/form-logic-components/FormBlockLogicEditor.vue b/client/components/open/forms/components/form-logic-components/FormBlockLogicEditor.vue
index 628981591..1ab331238 100644
--- a/client/components/open/forms/components/form-logic-components/FormBlockLogicEditor.vue
+++ b/client/components/open/forms/components/form-logic-components/FormBlockLogicEditor.vue
@@ -45,6 +45,9 @@
<h5 class="font-medium text-gray-700 mt-3">
2. Actions
</h5>
+ <p class="text-gray-500 text-xs mb-3">
+ Action(s) triggered when above conditions are true.
+ </p>
<flat-select-input
:key="resetKey"
v-model="logic.actions"
@@ -52,11 +55,14 @@
:multiple="true"
class="mt-1"
placeholder="Actions..."
- help="Action(s) triggered when above conditions are true"
:options="actionOptions"
@update:model-value="onActionInput"
/>
+ <p class="text-gray-500 text-xs mb-3">
+ Note that hidden fields can never be required.
+ </p>
+
<modal
max-width="sm"