Skip to content

Commit 863c5c5

Browse files
Add test for form logic field visibility and validation
- Implement test case for fields hidden by logic conditions - Update AnswerFormRequest to use new FormLogicPropertyResolver method for hidden field check - Ensure required fields are skipped when hidden by form logic
1 parent d9fcda0 commit 863c5c5

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

api/app/Http/Requests/AnswerFormRequest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ public function rules()
8787
$data[$field['id']] = isset($tmpop['name']) ? $tmpop['name'] : $data[$field['id']];
8888
}
8989
}
90-
if (FormLogicPropertyResolver::isRequired($property, $data) && (!isset($property['hidden']) || !$property['hidden'])) {
90+
if (
91+
FormLogicPropertyResolver::isRequired($property, $data) &&
92+
!FormLogicPropertyResolver::isHidden($property, $data)
93+
) {
9194
$rules[] = 'required';
9295

9396
if ($property['type'] == 'checkbox') {

api/tests/Feature/Forms/FormLogicTest.php

+64
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,67 @@
543543
'message' => $validationMessage,
544544
]);
545545
});
546+
547+
it('skips validation for fields hidden by logic conditions', function () {
548+
$user = $this->actingAsUser();
549+
$workspace = $this->createUserWorkspace($user);
550+
$form = $this->createForm($user, $workspace, [
551+
'properties' => [
552+
[
553+
'id' => 'title',
554+
'name' => 'Name',
555+
'type' => 'title',
556+
'hidden' => false,
557+
'required' => true,
558+
'logic' => [
559+
'conditions' => [
560+
'operatorIdentifier' => 'and',
561+
'children' => [
562+
[
563+
'identifier' => 'email',
564+
'value' => [
565+
'operator' => 'is_empty',
566+
'property_meta' => [
567+
'id' => 'email_field',
568+
'type' => 'email',
569+
],
570+
'value' => true,
571+
],
572+
],
573+
],
574+
],
575+
'actions' => ['hide-block'],
576+
],
577+
],
578+
[
579+
'id' => 'email_field',
580+
'name' => 'Email',
581+
'type' => 'email',
582+
'hidden' => false,
583+
'required' => false,
584+
],
585+
],
586+
]);
587+
588+
// Test when field should be hidden (email is empty)
589+
$formData = ['email_field' => '']; // Empty email
590+
$this->postJson(route('forms.answer', $form->slug), $formData)
591+
->assertSuccessful()
592+
->assertJson([
593+
'type' => 'success',
594+
'message' => 'Form submission saved.',
595+
]);
596+
597+
// Test when field should be visible (email is not empty)
598+
$formData = ['email_field' => 'test@example.com'];
599+
$this->postJson(route('forms.answer', $form->slug), $formData)
600+
->assertStatus(422)
601+
->assertJson([
602+
'message' => 'The Name field is required.',
603+
'errors' => [
604+
'title' => [
605+
'The Name field is required.',
606+
],
607+
],
608+
]);
609+
});

0 commit comments

Comments
 (0)