Skip to content

Instantly share code, notes, and snippets.

@zgchurch
Created December 13, 2011 14:41
Show Gist options
  • Save zgchurch/1472347 to your computer and use it in GitHub Desktop.
Save zgchurch/1472347 to your computer and use it in GitHub Desktop.
Adding little stars to the labels of required attributes
# 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
label.required:after {
color: red;
content: " *";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment