Created
October 12, 2011 04:05
-
-
Save mnylen/1280236 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/suppress_action_view_field_errors.rb | |
# The default form builder appends <div class="field_with_errors"> (or something similar) | |
# to all fields and labels for model attributes that have validation errors. Doesn't work | |
# so well with Bootstrap. | |
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| | |
"#{html_tag}".html_safe | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/twitter_bootstrap_form_builder.rb | |
class TwitterBootstrapFormBuilder < ActionView::Helpers::FormBuilder | |
FIELD_TYPES = { | |
:password => :password_field, | |
:email => :email_field, | |
:check_box => :check_box, | |
:text => :text_field | |
} | |
def field(attribute_name, field_type = nil, html_options = {}) | |
css_class = nil | |
if not self.object.errors[attribute_name].empty? | |
css_class = "error" | |
end | |
field_method = if not field_type | |
field_method = FIELD_TYPES[attribute_name] || "text_field" | |
else | |
field_method = FIELD_TYPES[field_type] || "text_field" | |
end | |
if html_options[:class] and css_class | |
html_options[:class] = "#{html_options[:class]} #{css_class}" | |
elsif css_class | |
html_options[:class] = css_class | |
end | |
label = self.label(attribute_name) | |
input = self.send(field_method, attribute_name, html_options) | |
input_container = if self.object.errors[attribute_name].empty? | |
@template.content_tag(:div, input, :class => "input") | |
else | |
inline_help = @template.content_tag(:span, self.object.errors[attribute_name].join(", "), :class => 'help-inline') | |
@template.content_tag(:div, input + inline_help, :class => "input") | |
end | |
@template.content_tag(:div, label + input_container, :class => "clearfix #{css_class}") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment