From ae526b30c7cfbdb49dab7b23af7b7f888682df13 Mon Sep 17 00:00:00 2001 From: Seth Veale Date: Fri, 19 Aug 2016 14:43:25 +1200 Subject: [PATCH] Added a minlength validation on first attempt? Wooo - wish I could put bangs in commit messages *bang* --- .gitignore | 1 + lib/simple_form/components.rb | 1 + lib/simple_form/components/minlength.rb | 34 ++++++++++++++++++++++++ lib/simple_form/inputs/base.rb | 3 ++- lib/simple_form/inputs/password_input.rb | 2 +- lib/simple_form/inputs/string_input.rb | 2 +- lib/simple_form/inputs/text_input.rb | 2 +- test/form_builder/input_field_test.rb | 8 ++++++ 8 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 lib/simple_form/components/minlength.rb diff --git a/.gitignore b/.gitignore index e464b991..40ccfffc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ pkg/ rdoc/ gemfiles/*.lock +/.idea/ diff --git a/lib/simple_form/components.rb b/lib/simple_form/components.rb index 457e2357..1c0dbc20 100644 --- a/lib/simple_form/components.rb +++ b/lib/simple_form/components.rb @@ -15,6 +15,7 @@ module SimpleForm autoload :Labels autoload :MinMax autoload :Maxlength + autoload :Minlength autoload :Pattern autoload :Placeholders autoload :Readonly diff --git a/lib/simple_form/components/minlength.rb b/lib/simple_form/components/minlength.rb new file mode 100644 index 00000000..8dc8d126 --- /dev/null +++ b/lib/simple_form/components/minlength.rb @@ -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 diff --git a/lib/simple_form/inputs/base.rb b/lib/simple_form/inputs/base.rb index 65fd1e91..3ae57e8f 100644 --- a/lib/simple_form/inputs/base.rb +++ b/lib/simple_form/inputs/base.rb @@ -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 diff --git a/lib/simple_form/inputs/password_input.rb b/lib/simple_form/inputs/password_input.rb index 0aa7385a..5378d104 100644 --- a/lib/simple_form/inputs/password_input.rb +++ b/lib/simple_form/inputs/password_input.rb @@ -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) diff --git a/lib/simple_form/inputs/string_input.rb b/lib/simple_form/inputs/string_input.rb index 4752be92..9a5177f7 100644 --- a/lib/simple_form/inputs/string_input.rb +++ b/lib/simple_form/inputs/string_input.rb @@ -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? diff --git a/lib/simple_form/inputs/text_input.rb b/lib/simple_form/inputs/text_input.rb index d90eedac..d7c4cfff 100644 --- a/lib/simple_form/inputs/text_input.rb +++ b/lib/simple_form/inputs/text_input.rb @@ -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) diff --git a/test/form_builder/input_field_test.rb b/test/form_builder/input_field_test.rb index a017cbe4..7da506ab 100644 --- a/test/form_builder/input_field_test.rb +++ b/test/form_builder/input_field_test.rb @@ -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