Skip to content

Commit ca5a141

Browse files
committed
added validation form builder (set as default form builder)
1 parent fe3d2be commit ca5a141

7 files changed

Lines changed: 102 additions & 217 deletions

File tree

app/helpers/admin/application_helper.rb

Lines changed: 0 additions & 35 deletions
This file was deleted.

app/helpers/application_helper.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module ApplicationHelper
2+
def flash_class(level)
3+
case level.intern
4+
when :notice then "alert alert-info"
5+
when :success then "alert alert-success"
6+
when :error then "alert alert-danger"
7+
when :alert then "alert alert-danger"
8+
end
9+
end
10+
11+
class ValidationFormBuilder < ActionView::Helpers::FormBuilder
12+
def validation_text_field(attribute, label_text, options = { column_width: 10 })
13+
validation_inline_label(attribute, label_text, { column_width: 12 - options[:column_width] }) do
14+
@template.text_field(@object_name, attribute, class: "form-control")
15+
end
16+
end
17+
18+
def validation_inline_label(attribute, label_text, options = { column_width: 2 }, &block)
19+
@template.content_tag(:div, class: "form-group form-group-lg has-feedback #{validation_state(@object, attribute)}") do
20+
content = @template.label(@object_name, attribute, label_text, class: "col-sm-#{options[:column_width]} control-label")
21+
content += @template.content_tag(:div, class: "col-sm-#{12 - options[:column_width]}") do
22+
content = @template.capture(&block)
23+
content += @template.content_tag(:span, "", class: "glyphicon #{validation_state_feedback(@object, attribute)} form-control-feedback")
24+
content += @template.content_tag(:span, class: "help-block") do
25+
attribute_error_message(@object, attribute)
26+
end
27+
content.html_safe
28+
end
29+
content.html_safe
30+
end
31+
end
32+
33+
private
34+
def validation_state(model_instance, attribute)
35+
if model_instance.errors.any?
36+
if model_instance.errors[attribute].empty?
37+
"has-success"
38+
else
39+
"has-error"
40+
end
41+
end
42+
end
43+
44+
def validation_state_feedback(model_instance, attribute)
45+
if model_instance.errors.any?
46+
if model_instance.errors[attribute].empty?
47+
"glyphicon-ok"
48+
else
49+
"glyphicon-remove"
50+
end
51+
end
52+
end
53+
54+
def attribute_error_message(model_instance, attribute)
55+
model_instance.errors[attribute].join('\n')
56+
end
57+
end
58+
end
Lines changed: 27 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,31 @@
1-
<div class="form-group form-group-lg has-feedback <%= validation_state(@notice, :title) %>">
2-
<%= f.label :title, "제목", class: "col-sm-1 control-label" %>
3-
<div class="col-sm-11">
4-
<%= f.text_field :title, class: "form-control" %>
5-
<span class="glyphicon <%= validation_state_feedback(@notice, :title) %> form-control-feedback"></span>
6-
<span class="help-block"><%= attribute_error_message(@notice, :title) %></span>
7-
</div>
8-
</div>
9-
10-
<div class="form-group form-group-lg has-feedback <%= validation_state(@notice, :content) %>">
11-
<%= f.label :content, "내용", class: "col-sm-1 control-label" %>
12-
<div class="col-sm-11">
13-
<%= f.text_field :content, class: "form-control" %>
14-
<span class="glyphicon <%= validation_state_feedback(@notice, :content) %> form-control-feedback"></span>
15-
<span class="help-block"><%= attribute_error_message(@notice, :content) %></span>
16-
</div>
17-
</div>
18-
19-
<div class="form-group form-group-lg has-feedback <%= validation_state(@notice, :link) %>">
20-
<%= f.label :link, "링크", class: "col-sm-1 control-label" %>
21-
<div class="col-sm-11">
22-
<%= f.text_field :link, class: "form-control" %>
23-
<span class="glyphicon <%= validation_state_feedback(@notice, :link) %> form-control-feedback"></span>
24-
<span class="help-block"><%= attribute_error_message(@notice, :link) %></span>
25-
</div>
26-
</div>
27-
28-
<div class="form-group form-group-lg has-feedback <%= validation_state(@notice, :notice_type) %>">
29-
<%= f.label :notice_type, "유형", class: "col-sm-1 control-label" %>
30-
<div class="col-sm-11">
31-
<%= f.label :notice_type_external, class: "radio-inline control-label" do %>
32-
<%= f.radio_button :notice_type, "external", checked: true, class: "notice-type-option" %>
33-
외부 공지
34-
<% end %>
35-
<%= f.label :notice_type_plain, class: "radio-inline control-label" do %>
36-
<%= f.radio_button :notice_type, "plain", class: "notice-type-option" %>
37-
내부 공지
38-
<% end %>
39-
<%= f.label :notice_type_survey, class: "radio-inline control-label" do %>
40-
<%= f.radio_button :notice_type, "survey", class: "notice-type-option" %>
41-
수요조사
1+
<%= f.validation_text_field :title, "제목", { column_width: 11 } %>
2+
<%= f.validation_text_field :content, "내용", { column_width: 11 } %>
3+
<%= f.validation_text_field :link, "링크", { column_width: 11 } %>
4+
<%= f.validation_inline_label :notice_type, "유형", { column_width: 1 } do %>
5+
<%= f.label :notice_type_external, class: "radio-inline control-label" do %>
6+
<%= f.radio_button :notice_type, "external", checked: true, class: "notice-type-option" %>
7+
외부 공지
8+
<% end %>
9+
<%= f.label :notice_type_plain, class: "radio-inline control-label" do %>
10+
<%= f.radio_button :notice_type, "plain", class: "notice-type-option" %>
11+
내부 공지
12+
<% end %>
13+
<%= f.label :notice_type_survey, class: "radio-inline control-label" do %>
14+
<%= f.radio_button :notice_type, "survey", class: "notice-type-option" %>
15+
수요조사
16+
<% end %>
17+
<%= f.label :notice_type_to, class: "radio-inline control-label" do %>
18+
<%= f.radio_button :notice_type, "to", class: "notice-type-option" %>
19+
TO 공지
4220
<% end %>
43-
<%= f.label :notice_type_to, class: "radio-inline control-label" do %>
44-
<%= f.radio_button :notice_type, "to", class: "notice-type-option" %>
45-
TO 공지
46-
<% end %>
47-
48-
<span class="glyphicon <%= validation_state_feedback(@notice, :notice_type) %> form-control-feedback"></span>
49-
<span class="help-block"><%= attribute_error_message(@notice, :notice_type) %></span>
50-
</div>
51-
</div>
52-
21+
<% end %>
5322
<div id="to-option">
54-
<div class="form-group form-group-lg has-feedback <%= validation_state(@notice, :to) %>">
55-
<%= f.label :to, "모집 인원", class: "col-sm-1 control-label" %>
56-
<div class="col-sm-11">
57-
<%= f.number_field :to, min: 1, class: "form-control" %>
58-
<span class="glyphicon <%= validation_state_feedback(@notice, :to) %> form-control-feedback"></span>
59-
<span class="help-block"><%= attribute_error_message(@notice, :to) %></span>
60-
</div>
61-
</div>
62-
63-
<div class="form-group form-group-lg has-feedback <%= validation_state(@notice, :due_date) %>">
64-
<%= f.label :due_date, "행사 날짜", class: "col-sm-1 control-label" %>
65-
<div class="col-sm-11 margin-top15">
66-
<%= f.date_select :due_date, {start_year: Date.today.year}, {class: "selectpicker"} %>
67-
<p class="text-success">날짜 3일 전까지 공개 모집이 가능합니다</p>
23+
<%= f.validation_inline_label :to, "모집 인원", { column_width: 1 } do %>
24+
<%= f.number_field :to, min: 1, class: "form-control" %>
25+
<% end %>
6826

69-
<span class="glyphicon <%= validation_state_feedback(@notice, :due_date) %> form-control-feedback"></span>
70-
<span class="help-block"><%= attribute_error_message(@notice, :due_date) %></span>
71-
</div>
72-
</div>
27+
<%= f.validation_inline_label :due_date, "행사 날짜", { column_width: 1 } do %>
28+
<%= f.date_select :due_date, {start_year: Date.today.year}, {class: "selectpicker"} %>
29+
<p class="text-success">날짜 3일 전까지 공개 모집이 가능합니다</p>
30+
<% end %>
7331
</div>
Lines changed: 13 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,13 @@
1-
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :generation_id) %>">
2-
<%= f.label :generation_id, "기수", class: "col-sm-2 control-label" %>
3-
<div class="col-sm-10">
4-
<%= f.text_field :generation_id, autofocus: true, class: "form-control" %>
5-
<span class="glyphicon <%= validation_state_feedback(@user, :generation_id) %> form-control-feedback"></span>
6-
<span class="help-block"><%= attribute_error_message(@user, :generation_id) %></span>
7-
</div>
8-
</div>
9-
10-
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :username) %>">
11-
<%= f.label :username, "이름", class: "col-sm-2 control-label" %>
12-
<div class="col-sm-10">
13-
<%= f.text_field :username, autofocus: true, class: "form-control" %>
14-
<span class="glyphicon <%= validation_state_feedback(@user, :username) %> form-control-feedback"></span>
15-
<span class="help-block"><%= attribute_error_message(@user, :username) %></span>
16-
</div>
17-
</div>
18-
19-
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :birth) %>">
20-
<%= f.label :birth, "생년월일", class: "col-sm-2 control-label" %>
21-
<div class="col-sm-10">
22-
<%= f.text_field :birth, autofocus: true, class: "form-control" %>
23-
<span class="glyphicon <%= validation_state_feedback(@user, :birth) %> form-control-feedback"></span>
24-
<span class="help-block"><%= attribute_error_message(@user, :birth) %></span>
25-
</div>
26-
</div>
27-
28-
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :sex) %>">
29-
<%= f.label :sex, "성별", class: "col-sm-2 control-label" %>
30-
<div class="col-sm-10">
31-
<%= f.text_field :sex, autofocus: true, class: "form-control" %>
32-
<span class="glyphicon <%= validation_state_feedback(@user, :sex) %> form-control-feedback"></span>
33-
<span class="help-block"><%= attribute_error_message(@user, :sex) %></span>
34-
</div>
35-
</div>
36-
37-
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :major) %>">
38-
<%= f.label :major, "전공", class: "col-sm-2 control-label" %>
39-
<div class="col-sm-10">
40-
<%= f.text_field :major, autofocus: true, class: "form-control" %>
41-
<span class="glyphicon <%= validation_state_feedback(@user, :major) %> form-control-feedback"></span>
42-
<span class="help-block"><%= attribute_error_message(@user, :major) %></span>
43-
</div>
44-
</div>
45-
46-
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :student_id) %>">
47-
<%= f.label :student_id, "학번", class: "col-sm-2 control-label" %>
48-
<div class="col-sm-10">
49-
<%= f.text_field :student_id, autofocus: true, class: "form-control" %>
50-
<span class="glyphicon <%= validation_state_feedback(@user, :student_id) %> form-control-feedback"></span>
51-
<span class="help-block"><%= attribute_error_message(@user, :student_id) %></span>
52-
</div>
53-
</div>
54-
55-
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :phone_number) %>">
56-
<%= f.label :phone_number, "본인 연락처", class: "col-sm-2 control-label" %>
57-
<div class="col-sm-10">
58-
<%= f.text_field :phone_number, value: pretty_phone_number(@user.phone_number), autofocus: true, class: "form-control" %>
59-
<span class="glyphicon <%= validation_state_feedback(@user, :phone_number) %> form-control-feedback"></span>
60-
<span class="help-block"><%= attribute_error_message(@user, :phone_number) %></span>
61-
</div>
62-
</div>
63-
64-
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :home_phone_number) %>">
65-
<%= f.label :home_phone_number, "자택 전화번호", class: "col-sm-2 control-label" %>
66-
<div class="col-sm-10">
67-
<%= f.text_field :home_phone_number, autofocus: true, class: "form-control" %>
68-
<span class="glyphicon <%= validation_state_feedback(@user, :home_phone_number) %> form-control-feedback"></span>
69-
<span class="help-block"><%= attribute_error_message(@user, :home_phone_number) %></span>
70-
</div>
71-
</div>
72-
73-
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :emergency_phone_number) %>">
74-
<%= f.label :emergency_phone_number, "응급시 연락처", class: "col-sm-2 control-label" %>
75-
<div class="col-sm-10">
76-
<%= f.text_field :emergency_phone_number, autofocus: true, class: "form-control" %>
77-
<span class="glyphicon <%= validation_state_feedback(@user, :emergency_phone_number) %> form-control-feedback"></span>
78-
<span class="help-block"><%= attribute_error_message(@user, :emergency_phone_number) %></span>
79-
</div>
80-
</div>
81-
82-
83-
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :email) %>">
84-
<%= f.label :email, "이메일", class: "col-sm-2 control-label" %>
85-
<div class="col-sm-10">
86-
<%= f.text_field :email, autofocus: true, class: "form-control" %>
87-
<span class="glyphicon <%= validation_state_feedback(@user, :email) %> form-control-feedback"></span>
88-
<span class="help-block"><%= attribute_error_message(@user, :email) %></span>
89-
</div>
90-
</div>
91-
92-
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :habitat_id) %>">
93-
<%= f.label :habitat_id, "해비타트 아이디", class: "col-sm-2 control-label" %>
94-
<div class="col-sm-10">
95-
<%= f.text_field :habitat_id, autofocus: true, class: "form-control" %>
96-
<span class="glyphicon <%= validation_state_feedback(@user, :habitat_id) %> form-control-feedback"></span>
97-
<span class="help-block"><%= attribute_error_message(@user, :habitat_id) %></span>
98-
</div>
99-
</div>
100-
101-
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :member_type) %>">
102-
<%= f.label :member_type, "단원구분", class: "col-sm-2 control-label" %>
103-
<div class="col-sm-10">
104-
<%= f.text_field :member_type, autofocus: true, class: "form-control" %>
105-
<span class="glyphicon <%= validation_state_feedback(@user, :member_type) %> form-control-feedback"></span>
106-
<span class="help-block"><%= attribute_error_message(@user, :member_type) %></span>
107-
</div>
108-
</div>
1+
<%= f.validation_text_field :generation_id, "기수" %>
2+
<%= f.validation_text_field :username, "이름" %>
3+
<%= f.validation_text_field :birth, "생년월일" %>
4+
<%= f.validation_text_field :sex, "성별" %>
5+
<%= f.validation_text_field :generation_id, "기수" %>
6+
<%= f.validation_text_field :major, "전공" %>
7+
<%= f.validation_text_field :student_id, "학번" %>
8+
<%= f.validation_text_field :phone_number, "본인 연락처" %>
9+
<%= f.validation_text_field :home_phone_number, "자택 전화번호" %>
10+
<%= f.validation_text_field :emergency_phone_number, "응급시 연락처" %>
11+
<%= f.validation_text_field :email, "이메일" %>
12+
<%= f.validation_text_field :habitat_id, "해비타트 아이디" %>
13+
<%= f.validation_text_field :member_type, "단원구분" %>

app/views/sessions/_form.html.erb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :phone_number) %>">
1+
<div class="form-group form-group-lg">
22
<%= f.label :phone_number, "전화번호", class: "control-label" %>
3-
<%= f.text_field :phone_number, class: "form-control" %>
4-
<span class="glyphicon <%= validation_state_feedback(@user, :phone_number) %> form-control-feedback"></span>
5-
<span class="help-block"><%= attribute_error_message(@user, :phone_number) %></span>
3+
<%= f.text_field :phone_number, class: "form-control" %>
64
</div>

config/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ class Application < Rails::Application
2525
config.autoload_paths += %W(#{config.root}/lib)
2626
config.autoload_paths += Dir["#{config.root}/lib/**/"]
2727
end
28-
end
28+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ActionView::Base.default_form_builder = ApplicationHelper::ValidationFormBuilder

0 commit comments

Comments
 (0)