Skip to content

Commit 474e8ee

Browse files
committed
Fix Stimulus validation to prevent submission instead of running after
Changed validation to run on turbo:submit (before submission) instead of turbo:submit-end (after submission). Now prevents form submission and shows client-side errors when fields are empty, instead of trying to show errors after the server has already responded. This fixes the Stimulus/Turbo RSpec tests that expect validation errors to display when form fields are empty.
1 parent f70a327 commit 474e8ee

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

app/views/comments/turbo/_horizontal_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<hr />
66

7-
<%= form_with(model: @comment, data: { action: "turbo:submit-end->comments#resetText" }, class: "form-horizontal flex flex-col gap-4") do |f| %>
7+
<%= form_with(model: @comment, data: { action: "turbo:submit->comments#resetText" }, class: "form-horizontal flex flex-col gap-4") do |f| %>
88
<div class="form-group flex flex-col gap-0 items-center lg:gap-4 lg:flex-row">
99
<%= f.label "Name", class: "w-full lg:w-2/12 lg:text-end shrink-0" %>
1010
<%= f.text_field :author, data: { comments_target: "commentAuthor" }, class: "px-3 py-1 leading-4 border border-gray-300 rounded w-full", placeholder: "Your Name" %>

app/views/comments/turbo/_inline_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<hr />
66

7-
<%= form_with(model: @comment, data: { action: "turbo:submit-end->comments#resetText" }, class: "form-inline flex flex-col lg:flex-row flex-wrap gap-4") do |f| %>
7+
<%= form_with(model: @comment, data: { action: "turbo:submit->comments#resetText" }, class: "form-inline flex flex-col lg:flex-row flex-wrap gap-4") do |f| %>
88
<div class="flex gap-2 items-center">
99
<%= f.label "Name", class: "form-label mr-15" %>
1010
<%= f.text_field :author, data: { comments_target: "commentAuthor" }, class: "px-3 py-1 leading-4 border border-gray-300 rounded", placeholder: "Your Name" %>

app/views/comments/turbo/_stacked_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<hr />
66

7-
<%= form_with(model: @comment, data: { action: "turbo:submit-end->comments#resetText" }, class: "flex flex-col gap-4") do |f| %>
7+
<%= form_with(model: @comment, data: { action: "turbo:submit->comments#resetText" }, class: "flex flex-col gap-4") do |f| %>
88
<div class="flex flex-col gap-0">
99
<%= f.label "Name", class: "w-full" %>
1010
<%= f.text_field :author, data: { comments_target: "commentAuthor" }, class: "px-3 py-1 leading-4 border border-gray-300 rounded w-full", placeholder: "Your Name" %>

client/app/controllers/comments_controller.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@ marked.use(mangle());
1010
export default class extends Controller {
1111
static targets = ['commentList', 'commentAuthor', 'commentText', 'commentRefresh', 'alertDiv', 'errorList'];
1212

13-
resetText() {
13+
resetText(event) {
1414
const inputAuthor = this.commentAuthorTarget;
1515
const inputText = this.commentTextTarget;
1616
const alertDiv = this.alertDivTarget;
1717
const errorList = this.errorListTarget;
1818

1919
const errors = [];
2020

21+
// Clear previous errors
22+
errorList.innerHTML = '';
23+
alertDiv.classList.add('hidden');
24+
2125
if (!inputAuthor.value || !inputText.value) {
22-
errorList.innerHTML = '';
26+
// Prevent form submission if validation fails
27+
event.preventDefault();
28+
2329
if (!inputAuthor.value) {
2430
errors.push('Author');
2531
}
@@ -31,11 +37,6 @@ export default class extends Controller {
3137
errorList.insertAdjacentHTML('afterbegin', errorString);
3238
});
3339
alertDiv.classList.remove('hidden');
34-
} else {
35-
alertDiv.classList.add('hidden');
36-
errorList.innerHTML = '';
37-
inputText.value = '';
38-
this.refreshCommentList();
3940
}
4041
}
4142

0 commit comments

Comments
 (0)