Added a minlength validation on first attempt? Wooo - wish I could put bangs in commit messages *bang*

This commit is contained in:
Seth Veale 2016-08-19 14:43:25 +12:00
parent 5f869ecf84
commit ae526b30c7
8 changed files with 49 additions and 4 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
pkg/
rdoc/
gemfiles/*.lock
/.idea/

View File

@ -15,6 +15,7 @@ module SimpleForm
autoload :Labels
autoload :MinMax
autoload :Maxlength
autoload :Minlength
autoload :Pattern
autoload :Placeholders
autoload :Readonly

View File

@ -0,0 +1,34 @@
module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Minlength
def minlength(wrapper_options = nil)
input_html_options[:minlength] ||= minimum_length_from_validation || limit
nil
end
private
def minimum_length_from_validation
minlength = options[:minlength]
if minlength.is_a?(String) || minlength.is_a?(Integer)
minlength
else
length_validator = find_length_validator
if length_validator && !has_tokenizer?(length_validator)
length_validator.options[:is] || length_validator.options[:minimum]
end
end
end
def find_length_validator
find_validator(:length)
end
def has_tokenizer?(length_validator)
length_validator.options[:tokenizer]
end
end
end
end

View File

@ -21,6 +21,7 @@ module SimpleForm
include SimpleForm::Components::HTML5
include SimpleForm::Components::LabelInput
include SimpleForm::Components::Maxlength
include SimpleForm::Components::Minlength
include SimpleForm::Components::MinMax
include SimpleForm::Components::Pattern
include SimpleForm::Components::Placeholders
@ -50,7 +51,7 @@ module SimpleForm
enable :hint
# Usually disabled, needs to be enabled explicitly passing true as option.
disable :maxlength, :placeholder, :pattern, :min_max
disable :maxlength, :minlength, :placeholder, :pattern, :min_max
def initialize(builder, attribute_name, column, input_type, options = {})
super

View File

@ -1,7 +1,7 @@
module SimpleForm
module Inputs
class PasswordInput < Base
enable :placeholder, :maxlength
enable :placeholder, :maxlength, :minlength
def input(wrapper_options = nil)
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

View File

@ -1,7 +1,7 @@
module SimpleForm
module Inputs
class StringInput < Base
enable :placeholder, :maxlength, :pattern
enable :placeholder, :maxlength, :minlength, :pattern
def input(wrapper_options = nil)
unless string?

View File

@ -1,7 +1,7 @@
module SimpleForm
module Inputs
class TextInput < Base
enable :placeholder, :maxlength
enable :placeholder, :maxlength, :minlength
def input(wrapper_options = nil)
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

View File

@ -151,6 +151,14 @@ class InputFieldTest < ActionView::TestCase
end
end
test 'build input_field without minlength component use the minlength string' do
swap_wrapper :default, custom_wrapper_with_html5_components do
with_input_field_for @user, :name, minlength: 5
assert_select 'input[minlength="5"]'
end
end
test 'build input_field without readonly component use the readonly string' do
swap_wrapper :default, custom_wrapper_with_html5_components do
with_input_field_for @user, :name, readonly: true