From 34bab0b9a7a0f824054ce86e0398371725498e3f Mon Sep 17 00:00:00 2001 From: abe33 Date: Wed, 22 Jan 2014 16:21:03 +0100 Subject: [PATCH] feat(inputs): adds support for model errors in inputs wrappers --- .../default_theme/wrappers/_boolean.html.haml | 19 +++++++++++++++++-- .../default_theme/wrappers/_default.html.haml | 18 ++++++++++++++++-- lib/awesome_form.rb | 6 ++++++ .../app/views/forms/with_errors.html.haml | 6 ++++++ spec/dummy/config/routes.rb | 1 + spec/features/form_errors_spec.rb | 15 +++++++++++++++ 6 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 spec/dummy/app/views/forms/with_errors.html.haml create mode 100644 spec/features/form_errors_spec.rb diff --git a/app/views/awesome_form/default_theme/wrappers/_boolean.html.haml b/app/views/awesome_form/default_theme/wrappers/_boolean.html.haml index 0f7d66d..cd81874 100644 --- a/app/views/awesome_form/default_theme/wrappers/_boolean.html.haml +++ b/app/views/awesome_form/default_theme/wrappers/_boolean.html.haml @@ -2,16 +2,31 @@ wrapper_html ||= {} label_html ||= {} + has_error = object.errors.messages[attribute_name].present? + + wrapper_error_class = has_error ? AwesomeForm.default_input_wrapper_error_class : nil + wrapper_attrs = { - class: AwesomeForm.default_input_wrapper_class, + class: [ + AwesomeForm.default_input_wrapper_class, + wrapper_error_class, + wrapper_html.delete(:class), + ].compact.join(' '), }.merge(wrapper_html) label_attrs = { for: builder.input_id(attribute_name), - class: AwesomeForm.default_label_class, + class: [ + AwesomeForm.default_label_class, + label_html.delete(:class) + ].compact.join(' '), }.merge(label_html) %div{wrapper_attrs} = yield %label{label_attrs} = builder.input_label(attribute_name) + + - if has_error + - object.errors.messages[attribute_name].each do |msg| + %span{ class: AwesomeForm.default_error_class }= msg diff --git a/app/views/awesome_form/default_theme/wrappers/_default.html.haml b/app/views/awesome_form/default_theme/wrappers/_default.html.haml index 4da17ac..aad36e0 100644 --- a/app/views/awesome_form/default_theme/wrappers/_default.html.haml +++ b/app/views/awesome_form/default_theme/wrappers/_default.html.haml @@ -2,16 +2,30 @@ wrapper_html ||= {} label_html ||= {} + has_error = object.errors.messages[attribute_name].present? + + wrapper_error_class = has_error ? AwesomeForm.default_input_wrapper_error_class : nil + wrapper_attrs = { - class: AwesomeForm.default_input_wrapper_class, + class: [ + AwesomeForm.default_input_wrapper_class, + wrapper_error_class, + wrapper_html.delete(:class), + ].compact.join(' '), }.merge(wrapper_html) label_attrs = { for: builder.input_id(attribute_name), - class: AwesomeForm.default_label_class, + class: [ + AwesomeForm.default_label_class, + label_html.delete(:class) + ].compact.join(' '), }.merge(label_html) %div{wrapper_attrs} %label{label_attrs} = builder.input_label(attribute_name) .controls= yield + - if has_error + - object.errors.messages[attribute_name].each do |msg| + %span{ class: AwesomeForm.default_error_class }= msg diff --git a/lib/awesome_form.rb b/lib/awesome_form.rb index db446e0..504626b 100644 --- a/lib/awesome_form.rb +++ b/lib/awesome_form.rb @@ -40,6 +40,12 @@ module AwesomeForm mattr_accessor :default_input_wrapper_class @@default_input_wrapper_class = 'field' + mattr_accessor :default_input_wrapper_error_class + @@default_input_wrapper_error_class = 'has-errors' + + mattr_accessor :default_error_class + @@default_error_class = 'inline-error' + mattr_accessor :legal_attributes @@legal_attributes = { input: %w(accept alt autocomplete autofocus checked dirname disabled form formaction formenctype formmethod formnovalidate formtarget height list max maxlength min multiple name pattern placeholder readonly required size src step type value width).map(&:to_sym), diff --git a/spec/dummy/app/views/forms/with_errors.html.haml b/spec/dummy/app/views/forms/with_errors.html.haml new file mode 100644 index 0000000..646d7aa --- /dev/null +++ b/spec/dummy/app/views/forms/with_errors.html.haml @@ -0,0 +1,6 @@ +- m = Universe.new +- m.valid? + += awesome_form_for m, url: '#' do |form| + = form.inputs + = form.actions diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index 83bdc5b..9c9fc05 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -14,5 +14,6 @@ get '/data', to: 'forms#data' get '/check_boxes', to: 'forms#check_boxes' get '/radios', to: 'forms#radios' + get '/with_errors', to: 'forms#with_errors' end diff --git a/spec/features/form_errors_spec.rb b/spec/features/form_errors_spec.rb new file mode 100644 index 0000000..d82e50b --- /dev/null +++ b/spec/features/form_errors_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +feature 'errors' do + scenario 'in a form that have errors' do + visit '/with_errors' + + match_content_of(page, ' + form + .field.has-errors + label + .controls + .inline-error + ') + end +end