From ca5a1419fd7eaf7b6a375297cabdc24827587878 Mon Sep 17 00:00:00 2001 From: Hong ChulJu Date: Wed, 15 Oct 2014 20:46:27 +0900 Subject: [PATCH] added validation form builder (set as default form builder) --- app/helpers/admin/application_helper.rb | 35 ------- app/helpers/application_helper.rb | 58 ++++++++++++ app/views/admin/notices/_form.html.erb | 96 ++++++------------- app/views/admin/users/_form.html.erb | 121 +++--------------------- app/views/sessions/_form.html.erb | 6 +- config/application.rb | 2 +- config/initializers/form_builder.rb | 1 + 7 files changed, 102 insertions(+), 217 deletions(-) delete mode 100644 app/helpers/admin/application_helper.rb create mode 100644 app/helpers/application_helper.rb create mode 100644 config/initializers/form_builder.rb diff --git a/app/helpers/admin/application_helper.rb b/app/helpers/admin/application_helper.rb deleted file mode 100644 index 8b9e633..0000000 --- a/app/helpers/admin/application_helper.rb +++ /dev/null @@ -1,35 +0,0 @@ -module Admin::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 - - 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 \ No newline at end of file diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb new file mode 100644 index 0000000..77cc005 --- /dev/null +++ b/app/helpers/application_helper.rb @@ -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 \ No newline at end of file diff --git a/app/views/admin/notices/_form.html.erb b/app/views/admin/notices/_form.html.erb index b565eb4..bab2f75 100644 --- a/app/views/admin/notices/_form.html.erb +++ b/app/views/admin/notices/_form.html.erb @@ -1,73 +1,31 @@ -
- <%= f.label :title, "제목", class: "col-sm-1 control-label" %> -
- <%= f.text_field :title, class: "form-control" %> - - <%= attribute_error_message(@notice, :title) %> -
-
- -
- <%= f.label :content, "내용", class: "col-sm-1 control-label" %> -
- <%= f.text_field :content, class: "form-control" %> - - <%= attribute_error_message(@notice, :content) %> -
-
- -
- <%= f.label :link, "링크", class: "col-sm-1 control-label" %> -
- <%= f.text_field :link, class: "form-control" %> - - <%= attribute_error_message(@notice, :link) %> -
-
- -
- <%= f.label :notice_type, "유형", class: "col-sm-1 control-label" %> -
- <%= 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 %> - - - <%= attribute_error_message(@notice, :notice_type) %> -
-
- +<% end %>
-
- <%= f.label :to, "모집 인원", class: "col-sm-1 control-label" %> -
- <%= f.number_field :to, min: 1, class: "form-control" %> - - <%= attribute_error_message(@notice, :to) %> -
-
- -
- <%= f.label :due_date, "행사 날짜", class: "col-sm-1 control-label" %> -
- <%= f.date_select :due_date, {start_year: Date.today.year}, {class: "selectpicker"} %> -

날짜 3일 전까지 공개 모집이 가능합니다

+ <%= f.validation_inline_label :to, "모집 인원", { column_width: 1 } do %> + <%= f.number_field :to, min: 1, class: "form-control" %> + <% end %> - - <%= attribute_error_message(@notice, :due_date) %> -
-
+ <%= f.validation_inline_label :due_date, "행사 날짜", { column_width: 1 } do %> + <%= f.date_select :due_date, {start_year: Date.today.year}, {class: "selectpicker"} %> +

날짜 3일 전까지 공개 모집이 가능합니다

+ <% end %>
\ No newline at end of file diff --git a/app/views/admin/users/_form.html.erb b/app/views/admin/users/_form.html.erb index 2688f68..6f13ac6 100644 --- a/app/views/admin/users/_form.html.erb +++ b/app/views/admin/users/_form.html.erb @@ -1,108 +1,13 @@ -
- <%= f.label :generation_id, "기수", class: "col-sm-2 control-label" %> -
- <%= f.text_field :generation_id, autofocus: true, class: "form-control" %> - - <%= attribute_error_message(@user, :generation_id) %> -
-
- -
- <%= f.label :username, "이름", class: "col-sm-2 control-label" %> -
- <%= f.text_field :username, autofocus: true, class: "form-control" %> - - <%= attribute_error_message(@user, :username) %> -
-
- -
- <%= f.label :birth, "생년월일", class: "col-sm-2 control-label" %> -
- <%= f.text_field :birth, autofocus: true, class: "form-control" %> - - <%= attribute_error_message(@user, :birth) %> -
-
- -
- <%= f.label :sex, "성별", class: "col-sm-2 control-label" %> -
- <%= f.text_field :sex, autofocus: true, class: "form-control" %> - - <%= attribute_error_message(@user, :sex) %> -
-
- -
- <%= f.label :major, "전공", class: "col-sm-2 control-label" %> -
- <%= f.text_field :major, autofocus: true, class: "form-control" %> - - <%= attribute_error_message(@user, :major) %> -
-
- -
- <%= f.label :student_id, "학번", class: "col-sm-2 control-label" %> -
- <%= f.text_field :student_id, autofocus: true, class: "form-control" %> - - <%= attribute_error_message(@user, :student_id) %> -
-
- -
- <%= f.label :phone_number, "본인 연락처", class: "col-sm-2 control-label" %> -
- <%= f.text_field :phone_number, value: pretty_phone_number(@user.phone_number), autofocus: true, class: "form-control" %> - - <%= attribute_error_message(@user, :phone_number) %> -
-
- -
- <%= f.label :home_phone_number, "자택 전화번호", class: "col-sm-2 control-label" %> -
- <%= f.text_field :home_phone_number, autofocus: true, class: "form-control" %> - - <%= attribute_error_message(@user, :home_phone_number) %> -
-
- -
- <%= f.label :emergency_phone_number, "응급시 연락처", class: "col-sm-2 control-label" %> -
- <%= f.text_field :emergency_phone_number, autofocus: true, class: "form-control" %> - - <%= attribute_error_message(@user, :emergency_phone_number) %> -
-
- - -
- <%= f.label :email, "이메일", class: "col-sm-2 control-label" %> -
- <%= f.text_field :email, autofocus: true, class: "form-control" %> - - <%= attribute_error_message(@user, :email) %> -
-
- -
- <%= f.label :habitat_id, "해비타트 아이디", class: "col-sm-2 control-label" %> -
- <%= f.text_field :habitat_id, autofocus: true, class: "form-control" %> - - <%= attribute_error_message(@user, :habitat_id) %> -
-
- -
- <%= f.label :member_type, "단원구분", class: "col-sm-2 control-label" %> -
- <%= f.text_field :member_type, autofocus: true, class: "form-control" %> - - <%= attribute_error_message(@user, :member_type) %> -
-
+<%= 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, "단원구분" %> \ No newline at end of file diff --git a/app/views/sessions/_form.html.erb b/app/views/sessions/_form.html.erb index 2af7ee8..9331599 100644 --- a/app/views/sessions/_form.html.erb +++ b/app/views/sessions/_form.html.erb @@ -1,6 +1,4 @@ -
+
<%= f.label :phone_number, "전화번호", class: "control-label" %> - <%= f.text_field :phone_number, class: "form-control" %> - - <%= attribute_error_message(@user, :phone_number) %> + <%= f.text_field :phone_number, class: "form-control" %>
\ No newline at end of file diff --git a/config/application.rb b/config/application.rb index 18bb7df..7f2c98a 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 \ No newline at end of file diff --git a/config/initializers/form_builder.rb b/config/initializers/form_builder.rb new file mode 100644 index 0000000..b99e194 --- /dev/null +++ b/config/initializers/form_builder.rb @@ -0,0 +1 @@ +ActionView::Base.default_form_builder = ApplicationHelper::ValidationFormBuilder \ No newline at end of file