Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
module FormBuilderHelpers
class Html5
def self.options_from(object, attribute)
return {} unless object.class.respond_to?(:validators_on)
{}.tap do |options|
options[:required] = true if object.class.validators_on(attribute).any? { |validator| validator.is_a?(ActiveModel::Validations::PresenceValidator) }
if validator = object.class.validators_on(attribute).select { |validator| validator.is_a?(ActiveModel::Validations::InclusionValidator) && validator.options[:in].is_a?(Range) }.last
options[:min] = validator.options[:in].min
options[:max] = validator.options[:in].max
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment