Created
December 13, 2011 14:41
-
-
Save zgchurch/1472347 to your computer and use it in GitHub Desktop.
Adding little stars to the labels of required attributes
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
# Partially stolen from: http://davidsulc.com/blog/2011/05/01/self-marking-required-fields-in-rails-3/ | |
class ActionView::Helpers::FormBuilder | |
alias :orig_label :label | |
# add a 'required' class to the field label if the field is required | |
def label(method, content_or_options = nil, options={}, &block) | |
if content_or_options && content_or_options.class == Hash | |
options = content_or_options | |
else | |
content = content_or_options | |
end | |
validations = object.class.validators_on(method).map(&:class) | |
if (validations & [ActiveModel::Validations::PresenceValidator, ActiveModel::Validations::NumericalityValidator]).any? | |
options[:class] = "#{options[:class]} required" | |
end | |
self.orig_label(method, content, options, &block) | |
end | |
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
label.required:after { | |
color: red; | |
content: " *"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment