Add support for maxlenght to text area inputs. Closes #232

This commit is contained in:
Rafael Mendonça França 2011-08-30 20:51:53 -03:00
parent eee7a60371
commit 4a4c99f7fa
10 changed files with 63 additions and 19 deletions

View File

@ -6,6 +6,7 @@
* Change form css class handling to only add the dom class when one is not given to the form call (by github.com/patrick99e99)
* Support for required attributs when action validations are present (by github.com/csegura)
* Don't generate `size` attribute for numeric input (by github.com/jasonmp85)
* Support for `maxlength` on text area inputs inferred from validation
* bug fix
* Fix bug when simple_fields_for is used with a hash like models and Rails 3.1

View File

@ -2,7 +2,7 @@
SimpleForm.setup do |config|
# Components used by the form builder to generate a complete input. You can remove
# any of them, change the order, or even add your own components to the stack.
# config.components = [ :placeholder, :label_input, :hint, :error ]
# config.components = [ :placeholder, :maxlength, :label_input, :hint, :error ]
# Default tag used on hints.
# config.hint_tag = :span

View File

@ -45,7 +45,7 @@ module SimpleForm
# Components used by the form builder.
mattr_accessor :components
@@components = [ :placeholder, :label_input, :hint, :error ]
@@components = [ :placeholder, :maxlength, :label_input, :hint, :error ]
# Series of attemps to detect a default label method for collection.
mattr_accessor :collection_label_methods

View File

@ -6,5 +6,6 @@ module SimpleForm
autoload :Labels, 'simple_form/components/labels'
autoload :Placeholders, 'simple_form/components/placeholders'
autoload :Wrapper, 'simple_form/components/wrapper'
autoload :Maxlength, 'simple_form/components/maxlength'
end
end
end

View File

@ -0,0 +1,32 @@
module SimpleForm
module Components
module Maxlength
def maxlength
if has_maxlength?
input_html_options[:maxlength] ||= maximum_length_from_validation
input_html_options[:maxlength] ||= limit if limit && SimpleForm.html5
end
nil
end
def has_maxlength?
false
end
def limit
column && column.limit
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

View File

@ -14,6 +14,7 @@ module SimpleForm
include SimpleForm::Components::LabelInput
include SimpleForm::Components::Placeholders
include SimpleForm::Components::Wrapper
include SimpleForm::Components::Maxlength
attr_reader :attribute_name, :column, :input_type, :reflection,
:options, :input_html_options

View File

@ -13,6 +13,10 @@ module SimpleForm
private
def has_maxlength?
text?
end
def has_placeholder?
(text? || password?) && placeholder_present?
end

View File

@ -8,8 +8,6 @@ module SimpleForm
def input
input_html_options[:size] ||= [limit, SimpleForm.default_input_size].compact.min
input_html_options[:maxlength] ||= maximum_length_from_validation
input_html_options[:maxlength] ||= limit if limit && SimpleForm.html5
if password? || SimpleForm.html5
input_html_options[:type] ||= input_type unless string?
end
@ -22,8 +20,8 @@ module SimpleForm
protected
def limit
column && column.limit
def has_maxlength?
true
end
def has_placeholder?
@ -37,17 +35,6 @@ module SimpleForm
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]
end
def find_length_validator
attribute_validators.find { |v| ActiveModel::Validations::LengthValidator === v }
end
end
end
end

View File

@ -398,6 +398,23 @@ class InputTest < ActionView::TestCase
assert_select 'textarea.text[placeholder=Put in some text]'
end
test 'input should get maxlength from column definition for text attributes' do
with_input_for @user, :description, :text
assert_select 'textarea.text[maxlength=200]'
end
test 'input should infer maxlength column definition from validation when present for text attributes' do
with_input_for @validating_user, :description, :text
assert_select 'textarea.text[maxlength=50]'
end
test 'when not using HTML5, does not show maxlength attribute for text attributes' do
swap SimpleForm, :html5 => false do
with_input_for @user, :description, :text
assert_no_select 'textarea.text[maxlength]'
end
end
test 'input should generate a file field' do
with_input_for @user, :name, :file
assert_select 'input#user_name[type=file]'

View File

@ -67,7 +67,7 @@ class User
def column_for_attribute(attribute)
column_type, limit = case attribute.to_sym
when :name, :status, :password then [:string, 100]
when :description then :text
when :description then [:text, 200]
when :age then :integer
when :credit_limit then [:decimal, 15]
when :active then :boolean
@ -144,6 +144,7 @@ class ValidatingUser < User
:less_than_or_equal_to => :max_attempts,
:only_integer => true
validates_length_of :name, :maximum => 25
validates_length_of :description, :maximum => 50
def min_amount
10