Created
July 14, 2012 21:10
-
-
Save graudeejs/3113362 to your computer and use it in GitHub Desktop.
Rails: add asterix to required fields
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
class ActionView::Helpers::FormBuilder | |
alias :orig_label :label | |
# add a '*' after the field label if the field is required | |
def label(method, content_or_options = nil, options = nil, &block) | |
if content_or_options && content_or_options.class == Hash | |
options = content_or_options | |
else | |
content = content_or_options | |
end | |
content ||= I18n.t("activerecord.attributes.#{object.class.name.underscore}.#{method}", :default => method.to_s.humanize) | |
if (options.nil? || options[:without_asterix] != true) && object.class.validators_on(method).map(&:class).include?(ActiveModel::Validations::PresenceValidator) | |
content += ' <em class="required">*</em>' | |
end | |
self.orig_label(method, content.html_safe, options || {}, &block) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment