extend form builder with error messages

This commit is contained in:
2015-02-17 12:05:44 +01:00
parent 26654165a1
commit 82670f271b
2 changed files with 12 additions and 1 deletions
@@ -11,6 +11,7 @@ class CustomFormBuilder < ActionView::Helpers::FormBuilder
)
t.content_tag(:div, elements.inject(&:+), class: 'form-actions')
end
def row(attribute, options={}, &block)
t = @template
classes = []
@@ -19,5 +20,15 @@ class CustomFormBuilder < ActionView::Helpers::FormBuilder
classes << 'error' if object.errors[attribute].present?
t.content_tag(:div, t.capture(&block), class: classes)
end
def error_message(attribute, options={}, &block)
if errors = object.errors[attribute].presence
t = @template
classes = ['error-message']
classes << attribute
classes |= Array.wrap(options[:class])
t.content_tag(:span, options[:message] || errors.join(', '), class: classes)
end
end
end
ActionView::Base.default_form_builder = CustomFormBuilder