Support for maxlength on string inputs inferred from validation #285

Merged
merged 3 commits into from Jul 21, 2011

Conversation

Projects
None yet
2 participants
@srbartlett
Contributor

srbartlett commented Jul 20, 2011

Added support for maxlength attribute to string input fields that declare validates_length_of.

Precedence is given to a field that declares a validates_length_of validation over the database column metadata limit.

The driver for this was having to repeat maxlength in both the model and the form which caught me out a few times!

Feedback welcome.

lib/simple_form/inputs/string_input.rb
@@ -8,11 +8,12 @@ module SimpleForm
def input
input_html_options[:size] ||= [limit, SimpleForm.default_input_size].compact.min
+ preferred_max_length = maximum_length_from_validation

This comment has been minimized.

Show comment Hide comment
@rafaelfranca

rafaelfranca Jul 20, 2011

Collaborator

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

input_html_options[:maxlength] ||= maximum_length_from_validation
@rafaelfranca

rafaelfranca Jul 20, 2011

Collaborator

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

input_html_options[:maxlength] ||= maximum_length_from_validation

This comment has been minimized.

Show comment Hide comment
@srbartlett

srbartlett Jul 21, 2011

Contributor

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

@srbartlett

srbartlett Jul 21, 2011

Contributor

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

lib/simple_form/inputs/string_input.rb
+ return unless has_validators?
+
+ length_validator = find_length_validator or return
+ length_validator.options[:maximum] if length_validator.options.key? :maximum

This comment has been minimized.

Show comment Hide comment
@rafaelfranca

rafaelfranca Jul 20, 2011

Collaborator

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

@rafaelfranca

rafaelfranca Jul 20, 2011

Collaborator

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

This comment has been minimized.

Show comment Hide comment
@srbartlett

srbartlett Jul 21, 2011

Contributor

Yep, I'll remove this if

@srbartlett

srbartlett Jul 21, 2011

Contributor

Yep, I'll remove this if

@rafaelfranca

This comment has been minimized.

Show comment Hide comment
@rafaelfranca

rafaelfranca Jul 20, 2011

Collaborator

Hi mate,

thank you for the pull request. I made some comments on the code.
Good work! And thank you again to help us with this gem.

❤️

Collaborator

rafaelfranca commented Jul 20, 2011

Hi mate,

thank you for the pull request. I made some comments on the code.
Good work! And thank you again to help us with this gem.

❤️

@srbartlett

This comment has been minimized.

Show comment Hide comment
@srbartlett

srbartlett Jul 21, 2011

Contributor

No problem. Thanks for your feedback and prompt response!

Let me know if you have any other suggestions.

cheers.

Contributor

srbartlett commented Jul 21, 2011

No problem. Thanks for your feedback and prompt response!

Let me know if you have any other suggestions.

cheers.

@srbartlett srbartlett closed this Jul 21, 2011

@rafaelfranca rafaelfranca reopened this Jul 21, 2011

rafaelfranca added a commit that referenced this pull request Jul 21, 2011

Merge pull request #285 from Jetstar/maxlength_on_inputs
Support for maxlength on string inputs inferred from validation

@rafaelfranca rafaelfranca merged commit bf01de6 into plataformatec:master Jul 21, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment