Skip to content

Instantly share code, notes, and snippets.

@shaunoconnell
Created August 19, 2011 13:05
Show Gist options
  • Save shaunoconnell/1156747 to your computer and use it in GitHub Desktop.
Save shaunoconnell/1156747 to your computer and use it in GitHub Desktop.
FormBuilder adding asterics
class RequiredFieldFormBuilder < ActionView::Helpers::FormBuilder
def label(method, text = nil, options = {}, &block)
new_text = text || method.to_s.titlecase
unless mark_required(object, method).nil?
new_text = "#{new_text} #{mark_required(object, method)}"
end
super(method,new_text, options, &block)
end
private
# stolen from http://asciicasts.com/episodes/211-validations-in-rails-3
def mark_required(object, attribute)
"*" if object.class.validators_on(attribute).map(&:class).include? ActiveModel::Validations::PresenceValidator
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment