Created
August 19, 2011 13:05
-
-
Save shaunoconnell/1156747 to your computer and use it in GitHub Desktop.
FormBuilder adding asterics
This file contains hidden or 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
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