Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/app/Http/Requests/AnswerFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public function rules()
$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') {
Expand Down
64 changes: 64 additions & 0 deletions api/tests/Feature/Forms/FormLogicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,67 @@
'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' => 'test@example.com'];
$this->postJson(route('forms.answer', $form->slug), $formData)
->assertStatus(422)
->assertJson([
'message' => 'The Name field is required.',
'errors' => [
'title' => [
'The Name field is required.',
],
],
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,24 @@
<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"
name="actions"
: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"

Expand Down
Loading