Skip to content

Instantly share code, notes, and snippets.

@graudeejs
Created July 14, 2012 21:10
Show Gist options
  • Save graudeejs/3113362 to your computer and use it in GitHub Desktop.
Save graudeejs/3113362 to your computer and use it in GitHub Desktop.
Rails: add asterix to required fields
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