Skip to content

Commit c55d0c3

Browse files
committed
feature/Add form controller to validate form inputs for submit
1 parent 298a268 commit c55d0c3

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Controller } from 'stimulus';
2+
3+
export default class extends Controller {
4+
static targets = ["field", "submit"];
5+
6+
connect() {
7+
this.initialValues = this.fieldTargets.map(target => target.value)
8+
}
9+
10+
validateInput(event) {
11+
const index = this.fieldTargets.indexOf(event.target)
12+
const initialValue = this.initialValues[index]
13+
14+
if (initialValue !== event.target.value) {
15+
this.enableSubmit()
16+
} else {
17+
if (this.changedFields().length > 0) {
18+
this.enableSubmit()
19+
} else {
20+
this.disableSubmit()
21+
}
22+
}
23+
}
24+
25+
enableSubmit() {
26+
this.submitTarget.classList.remove("button--cta-disabled")
27+
this.submitTarget.classList.add("button--cta-blue")
28+
this.submitTarget.disabled = false
29+
}
30+
31+
disableSubmit() {
32+
this.submitTarget.classList.remove("button--cta-blue")
33+
this.submitTarget.classList.add("button--cta-disabled")
34+
this.submitTarget.disabled = true
35+
}
36+
37+
changedFields() {
38+
return this.fieldTargets.filter(target => {
39+
const index = this.fieldTargets.indexOf(target)
40+
const initialValue = this.initialValues[index]
41+
42+
return initialValue !== target.value
43+
})
44+
}
45+
}

app/views/devise/registrations/edit.html.erb

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414
<div class="tabs-details">
1515
<div data-target="tabs.panel">
1616
<div class="margin-top">
17-
<%= form_for current_user, url: modify_users_path, html: { style: "width: 100%;", method: :put } do |f| %>
17+
<%= form_for current_user, url: modify_users_path, html: { style: "width: 100%;", data: { controller: "form" }, method: :put } do |f| %>
1818
<div class="edit-profile--wrapper">
1919
<div class="edit-profile--fields">
2020
<div class="form-field">
2121
<%= f.label :name %>
22-
<%= f.text_field :name %>
22+
<%= f.text_field :name, data: { target: "form.field", action: "input->form#validateInput" } %>
2323
</div>
2424

2525
<div class="form-field">
2626
<%= f.label :bio %>
27-
<%= f.text_area :bio %>
27+
<%= f.text_area :bio, data: { target: "form.field", action: "input->form#validateInput" } %>
2828
</div>
2929

3030
<div class="form-field">
3131
<%= f.label :location %>
32-
<%= f.text_field :location %>
32+
<%= f.text_field :location, data: { target: "form.field", action: "input->form#validateInput" } %>
3333
</div>
3434
</div>
3535

@@ -39,14 +39,13 @@
3939
class="edit-avatar--edit-button"
4040
data-action="click->upload#selectNewImage">
4141
<img src="/icons/edit.svg" width="16">
42-
<%= f.file_field :avatar, class: "hidden", data: { target: "upload.fileInput", action: "change->upload#handleFileSelect" } %>
43-
<%# <input data-target="upload.fileInput" data-action="change->upload#handleFileSelect" name="avatar" type="file" style="visibility: hidden" /> %>
42+
<%= f.file_field :avatar, class: "hidden", data: { target: "upload.fileInput form.field", action: "change->upload#handleFileSelect change->form#validateInput" } %>
4443
</div>
4544
</div>
4645
</div>
4746

4847
<div class="folders--options-wrapper">
49-
<%= f.submit "SAVE", class: 'button--cta-new' %>
48+
<%= f.submit "SAVE", class: 'button--cta-disabled', disabled: true, data: { target: "form.submit" } %>
5049
</div>
5150
<% end %>
5251

@@ -57,18 +56,6 @@
5756
</div>
5857

5958
<div data-target="tabs.panel" class="hidden">
60-
sec
61-
</div>
62-
</div>
63-
</div>
64-
65-
<card>
66-
<tabs title="Edit Profile">
67-
<tab name="Account" selected="true">
68-
<edit-profile />
69-
</tab>
70-
71-
<tab name="Security">
7259
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
7360
<%= render "devise/shared/error_messages", resource: resource %>
7461

@@ -110,6 +97,6 @@
11097
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
11198
<%= link_to "Back", :back %>
11299
<%= link_to('SIGN OUT', destroy_user_session_path, method: :delete, class: "button--cta-blue") %>
113-
</tab>
114-
</tabs>
115-
</card>
100+
</div>
101+
</div>
102+
</div>

0 commit comments

Comments
 (0)