Skip to content

Commit

Permalink
feat(inputs): adds support for model errors in inputs wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Jan 22, 2014
1 parent f8634f0 commit 34bab0b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
19 changes: 17 additions & 2 deletions app/views/awesome_form/default_theme/wrappers/_boolean.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 16 additions & 2 deletions app/views/awesome_form/default_theme/wrappers/_default.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions lib/awesome_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/app/views/forms/with_errors.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- m = Universe.new
- m.valid?

= awesome_form_for m, url: '#' do |form|
= form.inputs
= form.actions
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions spec/features/form_errors_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 34bab0b

Please sign in to comment.