Add support for maxlenght to text area inputs. Closes #232
This commit is contained in:
parent
eee7a60371
commit
4a4c99f7fa
|
@ -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)
|
* 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)
|
* 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)
|
* Don't generate `size` attribute for numeric input (by github.com/jasonmp85)
|
||||||
|
* Support for `maxlength` on text area inputs inferred from validation
|
||||||
|
|
||||||
* bug fix
|
* bug fix
|
||||||
* Fix bug when simple_fields_for is used with a hash like models and Rails 3.1
|
* Fix bug when simple_fields_for is used with a hash like models and Rails 3.1
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
SimpleForm.setup do |config|
|
SimpleForm.setup do |config|
|
||||||
# Components used by the form builder to generate a complete input. You can remove
|
# 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.
|
# 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.
|
# Default tag used on hints.
|
||||||
# config.hint_tag = :span
|
# config.hint_tag = :span
|
||||||
|
|
|
@ -45,7 +45,7 @@ module SimpleForm
|
||||||
|
|
||||||
# Components used by the form builder.
|
# Components used by the form builder.
|
||||||
mattr_accessor :components
|
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.
|
# Series of attemps to detect a default label method for collection.
|
||||||
mattr_accessor :collection_label_methods
|
mattr_accessor :collection_label_methods
|
||||||
|
|
|
@ -6,5 +6,6 @@ module SimpleForm
|
||||||
autoload :Labels, 'simple_form/components/labels'
|
autoload :Labels, 'simple_form/components/labels'
|
||||||
autoload :Placeholders, 'simple_form/components/placeholders'
|
autoload :Placeholders, 'simple_form/components/placeholders'
|
||||||
autoload :Wrapper, 'simple_form/components/wrapper'
|
autoload :Wrapper, 'simple_form/components/wrapper'
|
||||||
|
autoload :Maxlength, 'simple_form/components/maxlength'
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -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
|
|
@ -14,6 +14,7 @@ module SimpleForm
|
||||||
include SimpleForm::Components::LabelInput
|
include SimpleForm::Components::LabelInput
|
||||||
include SimpleForm::Components::Placeholders
|
include SimpleForm::Components::Placeholders
|
||||||
include SimpleForm::Components::Wrapper
|
include SimpleForm::Components::Wrapper
|
||||||
|
include SimpleForm::Components::Maxlength
|
||||||
|
|
||||||
attr_reader :attribute_name, :column, :input_type, :reflection,
|
attr_reader :attribute_name, :column, :input_type, :reflection,
|
||||||
:options, :input_html_options
|
:options, :input_html_options
|
||||||
|
|
|
@ -13,6 +13,10 @@ module SimpleForm
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def has_maxlength?
|
||||||
|
text?
|
||||||
|
end
|
||||||
|
|
||||||
def has_placeholder?
|
def has_placeholder?
|
||||||
(text? || password?) && placeholder_present?
|
(text? || password?) && placeholder_present?
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,8 +8,6 @@ module SimpleForm
|
||||||
|
|
||||||
def input
|
def input
|
||||||
input_html_options[:size] ||= [limit, SimpleForm.default_input_size].compact.min
|
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
|
if password? || SimpleForm.html5
|
||||||
input_html_options[:type] ||= input_type unless string?
|
input_html_options[:type] ||= input_type unless string?
|
||||||
end
|
end
|
||||||
|
@ -22,8 +20,8 @@ module SimpleForm
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def limit
|
def has_maxlength?
|
||||||
column && column.limit
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_placeholder?
|
def has_placeholder?
|
||||||
|
@ -37,17 +35,6 @@ module SimpleForm
|
||||||
def password?
|
def password?
|
||||||
input_type == :password
|
input_type == :password
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -398,6 +398,23 @@ class InputTest < ActionView::TestCase
|
||||||
assert_select 'textarea.text[placeholder=Put in some text]'
|
assert_select 'textarea.text[placeholder=Put in some text]'
|
||||||
end
|
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
|
test 'input should generate a file field' do
|
||||||
with_input_for @user, :name, :file
|
with_input_for @user, :name, :file
|
||||||
assert_select 'input#user_name[type=file]'
|
assert_select 'input#user_name[type=file]'
|
||||||
|
|
|
@ -67,7 +67,7 @@ class User
|
||||||
def column_for_attribute(attribute)
|
def column_for_attribute(attribute)
|
||||||
column_type, limit = case attribute.to_sym
|
column_type, limit = case attribute.to_sym
|
||||||
when :name, :status, :password then [:string, 100]
|
when :name, :status, :password then [:string, 100]
|
||||||
when :description then :text
|
when :description then [:text, 200]
|
||||||
when :age then :integer
|
when :age then :integer
|
||||||
when :credit_limit then [:decimal, 15]
|
when :credit_limit then [:decimal, 15]
|
||||||
when :active then :boolean
|
when :active then :boolean
|
||||||
|
@ -144,6 +144,7 @@ class ValidatingUser < User
|
||||||
:less_than_or_equal_to => :max_attempts,
|
:less_than_or_equal_to => :max_attempts,
|
||||||
:only_integer => true
|
:only_integer => true
|
||||||
validates_length_of :name, :maximum => 25
|
validates_length_of :name, :maximum => 25
|
||||||
|
validates_length_of :description, :maximum => 50
|
||||||
|
|
||||||
def min_amount
|
def min_amount
|
||||||
10
|
10
|
||||||
|
|
Loading…
Reference in New Issue