2011-08-30 19:51:53 -04:00
|
|
|
module SimpleForm
|
2011-09-03 06:00:23 -04:00
|
|
|
module Helpers
|
|
|
|
# Helper methods for maxlength.
|
|
|
|
module Maxlength #:nodoc:
|
2011-08-30 19:51:53 -04:00
|
|
|
|
2011-09-02 11:48:20 -04:00
|
|
|
private
|
|
|
|
|
2011-09-03 06:00:23 -04:00
|
|
|
def add_maxlength!
|
|
|
|
input_html_options[:maxlength] ||= maximum_length_from_validation || limit if SimpleForm.html5
|
2011-08-30 19:51:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def maximum_length_from_validation
|
|
|
|
return unless has_validators?
|
|
|
|
|
|
|
|
length_validator = find_length_validator or return
|
|
|
|
length_validator.options[:maximum]
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_length_validator
|
|
|
|
attribute_validators.find { |v| ActiveModel::Validations::LengthValidator === v }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|