Add default_form_class and allow form class overrides
This is especially useful with bootstrap forms where you might want to switch to a `form-inline` layout even though the default is a `form-horizontal`. This deprecates SimpleForm.form_class in favor of SimpleForm.default_form_class. e.g. config.default_form_class = "form-horizontal" simple_form_for @user, html: { class: "form-inline" }
This commit is contained in:
parent
ed3de1c2b5
commit
715f815f61
|
@ -103,8 +103,9 @@ SimpleForm.setup do |config|
|
||||||
# You can define the class to use on all labels. Default is nil.
|
# You can define the class to use on all labels. Default is nil.
|
||||||
# config.label_class = nil
|
# config.label_class = nil
|
||||||
|
|
||||||
# You can define the class to use on all forms. Default is simple_form.
|
# You can define the default class to be used on forms. Can be overriden
|
||||||
# config.form_class = :simple_form
|
# with `html: { :class }`. Defaulting to none.
|
||||||
|
# config.default_form_class = nil
|
||||||
|
|
||||||
# You can define which elements should obtain additional classes
|
# You can define which elements should obtain additional classes
|
||||||
# config.generate_additional_classes_for = [:wrapper, :label, :input]
|
# config.generate_additional_classes_for = [:wrapper, :label, :input]
|
||||||
|
|
|
@ -96,10 +96,16 @@ See https://github.com/plataformatec/simple_form/pull/997 for more information.
|
||||||
mattr_accessor :boolean_style
|
mattr_accessor :boolean_style
|
||||||
@@boolean_style = :inline
|
@@boolean_style = :inline
|
||||||
|
|
||||||
# You can define the class to be used on all forms. Default is simple_form.
|
# DEPRECATED: You can define the class to be used on all forms. Default is
|
||||||
|
# simple_form.
|
||||||
mattr_accessor :form_class
|
mattr_accessor :form_class
|
||||||
@@form_class = :simple_form
|
@@form_class = :simple_form
|
||||||
|
|
||||||
|
# You can define the default class to be used on all forms. Can be overriden
|
||||||
|
# with `html: { :class }`. Defaults to none.
|
||||||
|
mattr_accessor :default_form_class
|
||||||
|
@@default_form_class = nil
|
||||||
|
|
||||||
# You can define which elements should obtain additional classes.
|
# You can define which elements should obtain additional classes.
|
||||||
mattr_accessor :generate_additional_classes_for
|
mattr_accessor :generate_additional_classes_for
|
||||||
@@generate_additional_classes_for = [:wrapper, :label, :input]
|
@@generate_additional_classes_for = [:wrapper, :label, :input]
|
||||||
|
@ -249,6 +255,11 @@ See https://github.com/plataformatec/simple_form/pull/997 for more information.
|
||||||
ActiveSupport::Deprecation.warn "[SIMPLE_FORM] SimpleForm.default_input_size= is deprecated and has no effect", caller
|
ActiveSupport::Deprecation.warn "[SIMPLE_FORM] SimpleForm.default_input_size= is deprecated and has no effect", caller
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.form_class=(value)
|
||||||
|
ActiveSupport::Deprecation.warn "[SIMPLE_FORM] SimpleForm.form_class= is deprecated and will be removed in 4.x. Use SimpleForm.default_form_class= instead", caller
|
||||||
|
@@form_class = value
|
||||||
|
end
|
||||||
|
|
||||||
# Default way to setup Simple Form. Run rails generate simple_form:install
|
# Default way to setup Simple Form. Run rails generate simple_form:install
|
||||||
# to create a fresh initializer with all configuration values.
|
# to create a fresh initializer with all configuration values.
|
||||||
def self.setup
|
def self.setup
|
||||||
|
|
|
@ -16,7 +16,11 @@ module SimpleForm
|
||||||
unless options[:html].key?(:novalidate)
|
unless options[:html].key?(:novalidate)
|
||||||
options[:html][:novalidate] = !SimpleForm.browser_validations
|
options[:html][:novalidate] = !SimpleForm.browser_validations
|
||||||
end
|
end
|
||||||
options[:html][:class] = [SimpleForm.form_class, simple_form_css_class(record, options)].compact.join(" ")
|
if options[:html].key?(:class)
|
||||||
|
options[:html][:class] = [SimpleForm.form_class, options[:html][:class]].compact
|
||||||
|
else
|
||||||
|
options[:html][:class] = [SimpleForm.form_class, SimpleForm.default_form_class, simple_form_css_class(record, options)].compact
|
||||||
|
end
|
||||||
|
|
||||||
with_simple_form_field_error_proc do
|
with_simple_form_field_error_proc do
|
||||||
form_for(record, options, &block)
|
form_for(record, options, &block)
|
||||||
|
|
|
@ -13,6 +13,25 @@ class FormHelperTest < ActionView::TestCase
|
||||||
assert_select 'form.simple_form'
|
assert_select 'form.simple_form'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test 'SimpleForm allows overriding default form class' do
|
||||||
|
swap SimpleForm, default_form_class: "my_custom_class" do
|
||||||
|
with_concat_form_for :user, html: { class: "override_class" }
|
||||||
|
assert_no_select 'form.my_custom_class'
|
||||||
|
assert_select 'form.override_class'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Remove this test when SimpleForm.form_class is removed in 4.x
|
||||||
|
test 'SimpleForm allows overriding default form class, but not form class' do
|
||||||
|
ActiveSupport::Deprecation.silence do
|
||||||
|
swap SimpleForm, form_class: "fixed_class", default_form_class: "my_custom_class" do
|
||||||
|
with_concat_form_for :user, html: { class: "override_class" }
|
||||||
|
assert_no_select 'form.my_custom_class'
|
||||||
|
assert_select 'form.fixed_class.override_class'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
test 'SimpleForm uses default browser validations by default' do
|
test 'SimpleForm uses default browser validations by default' do
|
||||||
with_concat_form_for(:user)
|
with_concat_form_for(:user)
|
||||||
assert_no_select 'form[novalidate]'
|
assert_no_select 'form[novalidate]'
|
||||||
|
|
|
@ -217,12 +217,32 @@ class FormBuilderTest < ActionView::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
# COMMON OPTIONS
|
# COMMON OPTIONS
|
||||||
|
# Remove this test when SimpleForm.form_class is removed in 4.x
|
||||||
test 'builder adds chosen form class' do
|
test 'builder adds chosen form class' do
|
||||||
|
ActiveSupport::Deprecation.silence do
|
||||||
swap SimpleForm, form_class: :my_custom_class do
|
swap SimpleForm, form_class: :my_custom_class do
|
||||||
with_form_for @user, :name
|
with_form_for @user, :name
|
||||||
assert_select 'form.my_custom_class'
|
assert_select 'form.my_custom_class'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Remove this test when SimpleForm.form_class is removed in 4.x
|
||||||
|
test 'builder adds chosen form class and default form class' do
|
||||||
|
ActiveSupport::Deprecation.silence do
|
||||||
|
swap SimpleForm, form_class: "my_custom_class", default_form_class: "my_default_class" do
|
||||||
|
with_form_for @user, :name
|
||||||
|
assert_select 'form.my_custom_class.my_default_class'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'builder adds default form class' do
|
||||||
|
swap SimpleForm, default_form_class: "default_class" do
|
||||||
|
with_form_for @user, :name
|
||||||
|
assert_select 'form.default_class'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
test 'builder allows passing options to input' do
|
test 'builder allows passing options to input' do
|
||||||
with_form_for @user, :name, input_html: { class: 'my_input', id: 'my_input' }
|
with_form_for @user, :name, input_html: { class: 'my_input', id: 'my_input' }
|
||||||
|
|
Loading…
Reference in New Issue