class Forme::ErrorHandler::Bootstrap3

  1. lib/forme/bs3.rb
Superclass: ErrorHandler

BS3 Boostrap formatted error handler which adds a span tag with “help-block with-errors” classes for the error message.

Uses [github.com/1000hz/bootstrap-validator] formatting.

Note! The default “error” class on the input is being removed.

Registered as :bs3.

Methods

Public Instance

  1. call

Public Instance methods

call (tag, input)

Return tag with error message span tag after it.

[show source]
   # File lib/forme/bs3.rb
18 def call(tag, input)
19   if tag.is_a?(Tag)
20     tag.attr[:class] = tag.attr[:class].to_s.gsub(/\s*error\s*/,'')
21     tag.attr.delete(:class) if tag.attr[:class].to_s == ''
22   end
23   attr = input.opts[:error_attr]
24   attr = attr ? attr.dup : {}
25   Forme.attr_classes(attr, 'help-block with-errors')
26   return [tag] if input.opts[:skip_error_message]
27 
28   case input.type
29   when :submit, :reset
30     [tag]
31   when :textarea
32     input.opts[:wrapper] = :bs3
33     if input.opts[:wrapper_attr]
34       Forme.attr_classes(input.opts[:wrapper_attr], 'has-error')
35     else
36       input.opts[:wrapper_attr] = { :class => 'has-error' }
37     end
38     [ tag, input.tag(:span, attr, input.opts[:error]) ]
39     
40   when :select
41     input.opts[:wrapper] = :bs3
42     if input.opts[:wrapper_attr]
43       Forme.attr_classes(input.opts[:wrapper_attr], 'has-error')
44     else
45       input.opts[:wrapper_attr] = { :class => 'has-error' }
46     end
47     [ tag, input.tag(:span, attr, input.opts[:error]) ]
48     
49   when :checkbox, :radio
50     
51     input.opts[:wrapper] = :div
52     if input.opts[:wrapper_attr]
53       Forme.attr_classes(input.opts[:wrapper_attr], 'has-error')
54     else
55       input.opts[:wrapper_attr] = { :class => 'has-error' }
56     end
57     
58     [ 
59       input.tag(:div, { :class=> input.type.to_s }, [tag] ), 
60       input.tag(:span, attr, input.opts[:error]) 
61     ]
62   else
63     if input.opts[:wrapper_attr]
64       Forme.attr_classes(input.opts[:wrapper_attr], 'has-error')
65     else
66       input.opts[:wrapper_attr] = { :class => 'has-error' }
67     end
68     [tag, input.tag(:span, attr, input.opts[:error])]
69   end
70 end