Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for maxlength on string inputs inferred from validation #285

Merged
merged 3 commits into from Jul 21, 2011
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/simple_form/inputs/string_input.rb
Expand Up @@ -8,11 +8,12 @@ class StringInput < Base

def input
input_html_options[:size] ||= [limit, SimpleForm.default_input_size].compact.min
preferred_max_length = maximum_length_from_validation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you can avoid this line changing the next to:

input_html_options[:maxlength] ||= maximum_length_from_validation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are correct. I'll make this change.

input_html_options[:maxlength] ||= preferred_max_length if preferred_max_length
input_html_options[:maxlength] ||= limit if limit && SimpleForm.html5
if password? || SimpleForm.html5
input_html_options[:type] ||= input_type unless string?
end

@builder.send(input_method, attribute_name, input_html_options)
end

Expand All @@ -37,6 +38,17 @@ def string?
def password?
input_type == :password
end

def maximum_length_from_validation
return unless has_validators?

length_validator = find_length_validator or return
length_validator.options[:maximum] if length_validator.options.key? :maximum
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also avoid this if. If the hash doesn't have the key it will only returns nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I'll remove this if

end

def find_length_validator
attribute_validators.find { |v| ActiveModel::Validations::LengthValidator === v }
end
end
end
end
5 changes: 5 additions & 0 deletions test/inputs_test.rb
Expand Up @@ -166,6 +166,11 @@ def with_input_for(object, attribute_name, type, options={})
assert_select 'input.password[type=password][maxlength=100]'
end

test 'input should infer maxlength column definition from validation when present' do
with_input_for @validating_user, :name, :string
assert_select 'input.string[maxlength=25]'
end

test 'when not using HTML5, does not show maxlength attribute' do
swap SimpleForm, :html5 => false do
with_input_for @user, :password, :password
Expand Down
1 change: 1 addition & 0 deletions test/support/models.rb
Expand Up @@ -141,6 +141,7 @@ class ValidatingUser < User
:greater_than_or_equal_to => :min_attempts,
:less_than_or_equal_to => :max_attempts,
:only_integer => true
validates_length_of :name, :maximum => 25

def min_amount
10
Expand Down