Skip to content

Commit

Permalink
added validation form builder (set as default form builder)
Browse files Browse the repository at this point in the history
  • Loading branch information
angdev committed Oct 15, 2014
1 parent fe3d2be commit ca5a141
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 217 deletions.
35 changes: 0 additions & 35 deletions app/helpers/admin/application_helper.rb

This file was deleted.

58 changes: 58 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module ApplicationHelper
def flash_class(level)
case level.intern
when :notice then "alert alert-info"
when :success then "alert alert-success"
when :error then "alert alert-danger"
when :alert then "alert alert-danger"
end
end

class ValidationFormBuilder < ActionView::Helpers::FormBuilder
def validation_text_field(attribute, label_text, options = { column_width: 10 })
validation_inline_label(attribute, label_text, { column_width: 12 - options[:column_width] }) do
@template.text_field(@object_name, attribute, class: "form-control")
end
end

def validation_inline_label(attribute, label_text, options = { column_width: 2 }, &block)
@template.content_tag(:div, class: "form-group form-group-lg has-feedback #{validation_state(@object, attribute)}") do
content = @template.label(@object_name, attribute, label_text, class: "col-sm-#{options[:column_width]} control-label")
content += @template.content_tag(:div, class: "col-sm-#{12 - options[:column_width]}") do
content = @template.capture(&block)
content += @template.content_tag(:span, "", class: "glyphicon #{validation_state_feedback(@object, attribute)} form-control-feedback")
content += @template.content_tag(:span, class: "help-block") do
attribute_error_message(@object, attribute)
end
content.html_safe
end
content.html_safe
end
end

private
def validation_state(model_instance, attribute)
if model_instance.errors.any?
if model_instance.errors[attribute].empty?
"has-success"
else
"has-error"
end
end
end

def validation_state_feedback(model_instance, attribute)
if model_instance.errors.any?
if model_instance.errors[attribute].empty?
"glyphicon-ok"
else
"glyphicon-remove"
end
end
end

def attribute_error_message(model_instance, attribute)
model_instance.errors[attribute].join('\n')
end
end
end
96 changes: 27 additions & 69 deletions app/views/admin/notices/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,73 +1,31 @@
<div class="form-group form-group-lg has-feedback <%= validation_state(@notice, :title) %>">
<%= f.label :title, "제목", class: "col-sm-1 control-label" %>
<div class="col-sm-11">
<%= f.text_field :title, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@notice, :title) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@notice, :title) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@notice, :content) %>">
<%= f.label :content, "내용", class: "col-sm-1 control-label" %>
<div class="col-sm-11">
<%= f.text_field :content, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@notice, :content) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@notice, :content) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@notice, :link) %>">
<%= f.label :link, "링크", class: "col-sm-1 control-label" %>
<div class="col-sm-11">
<%= f.text_field :link, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@notice, :link) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@notice, :link) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@notice, :notice_type) %>">
<%= f.label :notice_type, "유형", class: "col-sm-1 control-label" %>
<div class="col-sm-11">
<%= f.label :notice_type_external, class: "radio-inline control-label" do %>
<%= f.radio_button :notice_type, "external", checked: true, class: "notice-type-option" %>
외부 공지
<% end %>
<%= f.label :notice_type_plain, class: "radio-inline control-label" do %>
<%= f.radio_button :notice_type, "plain", class: "notice-type-option" %>
내부 공지
<% end %>
<%= f.label :notice_type_survey, class: "radio-inline control-label" do %>
<%= f.radio_button :notice_type, "survey", class: "notice-type-option" %>
수요조사
<%= f.validation_text_field :title, "제목", { column_width: 11 } %>
<%= f.validation_text_field :content, "내용", { column_width: 11 } %>
<%= f.validation_text_field :link, "링크", { column_width: 11 } %>
<%= f.validation_inline_label :notice_type, "유형", { column_width: 1 } do %>
<%= f.label :notice_type_external, class: "radio-inline control-label" do %>
<%= f.radio_button :notice_type, "external", checked: true, class: "notice-type-option" %>
외부 공지
<% end %>
<%= f.label :notice_type_plain, class: "radio-inline control-label" do %>
<%= f.radio_button :notice_type, "plain", class: "notice-type-option" %>
내부 공지
<% end %>
<%= f.label :notice_type_survey, class: "radio-inline control-label" do %>
<%= f.radio_button :notice_type, "survey", class: "notice-type-option" %>
수요조사
<% end %>
<%= f.label :notice_type_to, class: "radio-inline control-label" do %>
<%= f.radio_button :notice_type, "to", class: "notice-type-option" %>
TO 공지
<% end %>
<%= f.label :notice_type_to, class: "radio-inline control-label" do %>
<%= f.radio_button :notice_type, "to", class: "notice-type-option" %>
TO 공지
<% end %>

<span class="glyphicon <%= validation_state_feedback(@notice, :notice_type) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@notice, :notice_type) %></span>
</div>
</div>

<% end %>
<div id="to-option">
<div class="form-group form-group-lg has-feedback <%= validation_state(@notice, :to) %>">
<%= f.label :to, "모집 인원", class: "col-sm-1 control-label" %>
<div class="col-sm-11">
<%= f.number_field :to, min: 1, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@notice, :to) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@notice, :to) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@notice, :due_date) %>">
<%= f.label :due_date, "행사 날짜", class: "col-sm-1 control-label" %>
<div class="col-sm-11 margin-top15">
<%= f.date_select :due_date, {start_year: Date.today.year}, {class: "selectpicker"} %>
<p class="text-success">날짜 3일 전까지 공개 모집이 가능합니다</p>
<%= f.validation_inline_label :to, "모집 인원", { column_width: 1 } do %>
<%= f.number_field :to, min: 1, class: "form-control" %>
<% end %>

<span class="glyphicon <%= validation_state_feedback(@notice, :due_date) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@notice, :due_date) %></span>
</div>
</div>
<%= f.validation_inline_label :due_date, "행사 날짜", { column_width: 1 } do %>
<%= f.date_select :due_date, {start_year: Date.today.year}, {class: "selectpicker"} %>
<p class="text-success">날짜 3일 전까지 공개 모집이 가능합니다</p>
<% end %>
</div>
121 changes: 13 additions & 108 deletions app/views/admin/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,108 +1,13 @@
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :generation_id) %>">
<%= f.label :generation_id, "기수", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_field :generation_id, autofocus: true, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@user, :generation_id) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@user, :generation_id) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :username) %>">
<%= f.label :username, "이름", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_field :username, autofocus: true, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@user, :username) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@user, :username) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :birth) %>">
<%= f.label :birth, "생년월일", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_field :birth, autofocus: true, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@user, :birth) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@user, :birth) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :sex) %>">
<%= f.label :sex, "성별", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_field :sex, autofocus: true, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@user, :sex) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@user, :sex) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :major) %>">
<%= f.label :major, "전공", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_field :major, autofocus: true, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@user, :major) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@user, :major) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :student_id) %>">
<%= f.label :student_id, "학번", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_field :student_id, autofocus: true, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@user, :student_id) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@user, :student_id) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :phone_number) %>">
<%= f.label :phone_number, "본인 연락처", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_field :phone_number, value: pretty_phone_number(@user.phone_number), autofocus: true, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@user, :phone_number) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@user, :phone_number) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :home_phone_number) %>">
<%= f.label :home_phone_number, "자택 전화번호", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_field :home_phone_number, autofocus: true, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@user, :home_phone_number) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@user, :home_phone_number) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :emergency_phone_number) %>">
<%= f.label :emergency_phone_number, "응급시 연락처", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_field :emergency_phone_number, autofocus: true, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@user, :emergency_phone_number) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@user, :emergency_phone_number) %></span>
</div>
</div>


<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :email) %>">
<%= f.label :email, "이메일", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_field :email, autofocus: true, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@user, :email) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@user, :email) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :habitat_id) %>">
<%= f.label :habitat_id, "해비타트 아이디", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_field :habitat_id, autofocus: true, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@user, :habitat_id) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@user, :habitat_id) %></span>
</div>
</div>

<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :member_type) %>">
<%= f.label :member_type, "단원구분", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.text_field :member_type, autofocus: true, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@user, :member_type) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@user, :member_type) %></span>
</div>
</div>
<%= f.validation_text_field :generation_id, "기수" %>
<%= f.validation_text_field :username, "이름" %>
<%= f.validation_text_field :birth, "생년월일" %>
<%= f.validation_text_field :sex, "성별" %>
<%= f.validation_text_field :generation_id, "기수" %>
<%= f.validation_text_field :major, "전공" %>
<%= f.validation_text_field :student_id, "학번" %>
<%= f.validation_text_field :phone_number, "본인 연락처" %>
<%= f.validation_text_field :home_phone_number, "자택 전화번호" %>
<%= f.validation_text_field :emergency_phone_number, "응급시 연락처" %>
<%= f.validation_text_field :email, "이메일" %>
<%= f.validation_text_field :habitat_id, "해비타트 아이디" %>
<%= f.validation_text_field :member_type, "단원구분" %>
6 changes: 2 additions & 4 deletions app/views/sessions/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<div class="form-group form-group-lg has-feedback <%= validation_state(@user, :phone_number) %>">
<div class="form-group form-group-lg">
<%= f.label :phone_number, "전화번호", class: "control-label" %>
<%= f.text_field :phone_number, class: "form-control" %>
<span class="glyphicon <%= validation_state_feedback(@user, :phone_number) %> form-control-feedback"></span>
<span class="help-block"><%= attribute_error_message(@user, :phone_number) %></span>
<%= f.text_field :phone_number, class: "form-control" %>
</div>
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ class Application < Rails::Application
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
end
end
end
1 change: 1 addition & 0 deletions config/initializers/form_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ActionView::Base.default_form_builder = ApplicationHelper::ValidationFormBuilder

0 comments on commit ca5a141

Please sign in to comment.