diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f7d4c2b..40b39c9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ ## 2.1.0.dev ### enhancements + * `input_field` supports components that don't generate tags + as `:min_max`, `:maxlength`, `:placeholder`, `:pattern`, `:readonly`. + Closes[#362](https://github.com/plataformatec/simple_form/issues/632). + ([@nashby](https://github.com/nashby)) * support for Rails eager loading. Closes [#478](https://github.com/plataformatec/simple_form/issues/478). * generate required attribute for `date_time` input. diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 0502a099..9c1035ad 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -138,7 +138,7 @@ module SimpleForm options[:input_html] = options.except(:as, :collection, :label_method, :value_method) options = @defaults.deep_dup.deep_merge(options) if @defaults - SimpleForm::Wrappers::Root.new([:input], wrapper: false).render find_input(attribute_name, options) + SimpleForm::Wrappers::Root.new([:min_max, :maxlength, :placeholder, :pattern, :readonly, :input], wrapper: false).render find_input(attribute_name, options) end # Helper for dealing with association selects/radios, generating the diff --git a/test/form_builder/input_field_test.rb b/test/form_builder/input_field_test.rb index a4488c42..3c969803 100644 --- a/test/form_builder/input_field_test.rb +++ b/test/form_builder/input_field_test.rb @@ -50,6 +50,51 @@ class InputFieldTest < ActionView::TestCase assert_no_select 'input.integer[as]' end + test 'builder input_field should use i18n to translate placeholder text' do + store_translations(:en, simple_form: { placeholders: { user: { + name: 'Name goes here' + } } }) do + + with_concat_form_for(@user) do |f| + f.input_field :name + end + + assert_select 'input.string[placeholder=Name goes here]' + end + end + + test 'builder input_field should use min_max component' do + with_concat_form_for(@other_validating_user) do |f| + f.input_field :age, as: :integer + end + + assert_select 'input[min=18]' + end + + test 'builder input_field should use pattern component' do + with_concat_form_for(@other_validating_user) do |f| + f.input_field :country, as: :string + end + + assert_select 'input[pattern="\w+"]' + end + + test 'builder input_field should use readonly component' do + with_concat_form_for(@other_validating_user) do |f| + f.input_field :age, as: :integer, readonly: true + end + + assert_select 'input.integer.readonly[readonly]' + end + + test 'builder input_field should use maxlength component' do + with_concat_form_for(@validating_user) do |f| + f.input_field :name, as: :string + end + + assert_select 'input.string[maxlength=25]' + end + test 'builder collection input_field should generate input tag with a clean HTML' do with_concat_form_for(@user) do |f| f.input_field :status, collection: ['Open', 'Closed'], class: 'status', label_method: :to_s, value_method: :to_s