Embed URL


SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Formatting Rails form elements for Twitter Bootstrap error validation

View application.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
ActionView::Base.field_error_proc = do |html_tag, instance|
html = %(<div class="field_with_errors">#{html_tag}</div>).html_safe
# add nokogiri gem to Gemfile
elements = Nokogiri::HTML::DocumentFragment.parse(html_tag).css "label, input"
elements.each do |e|
if e.node_name.eql? 'label'
html = %(<div class="clearfix error">#{e}</div>).html_safe
elsif e.node_name.eql? 'input'
if instance.error_message.kind_of?(Array)
html = %(<div class="clearfix error">#{html_tag}<span class="help-inline">&nbsp;#{instance.error_message.join(',')}</span></div>).html_safe
html = %(<div class="clearfix error">#{html_tag}<span class="help-inline">&nbsp;#{instance.error_message}</span></div>).html_safe
View application.rb
1 2 3 4 5 6 7
// Place all the styles related to the YourController controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here:
form .clearfix:before, form .clearfix:after {
display: inline;

Wow, great, it works excellent, thank you very much Trent!

t2 commented

Glad it helped!

I forked your version to and it now does also textarea validation with latest Bootstrap.

The error message is below the error field, i.e. in a second line. How to place it in the same line as the error field?

This is great, thanks.

Forked to for Zurb Foundation 4. This version removes the Nokogiri dependency and thus performs better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.