1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00

Add required_by_default as option.

This commit is contained in:
José Valim 2010-07-06 11:28:23 +02:00
parent ab60a080b3
commit dca86314fc
4 changed files with 23 additions and 10 deletions

View file

@ -1,10 +1,8 @@
# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
# 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 in the
# stack. By inheriting your component from SimpleForm::Components::Base you'll
# have some extra helpers for free.
# 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.
# config.components = [ :label_input, :hint, :error ]
# Default tag used on hints.
@ -16,19 +14,22 @@ SimpleForm.setup do |config|
# You can wrap all inputs in a pre-defined tag.
# config.wrapper_tag = :div
# CSS class to add to all wrapper tags
# CSS class to add to all wrapper tags.
# config.wrapper_class = :input
# CSS class to add to the wrapper if the field has errors
# CSS class to add to the wrapper if the field has errors.
# config.wrapper_error_class = :fieldWithErrors
# How the label text should be generated altogether with the required text.
# config.label_text = lambda { |label, required| "#{required} #{label}" }
# Series of attemps to detect a default label method for collection
# Whether attributes are required by default (or not). Default is true.
# config.required_by_default = true
# Series of attemps to detect a default label method for collection.
# config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
# Series of attemps to detect a default value method for collection
# Series of attemps to detect a default value method for collection.
# config.collection_value_methods = [ :id, :to_s ]
# Collection of methods to detect if a file type was given.
@ -40,6 +41,6 @@ SimpleForm.setup do |config|
# Default priority for country inputs.
# config.country_priority = nil
# Default size for text inputs
# Default size for text inputs.
# config.default_input_size = 50
end

View file

@ -45,6 +45,10 @@ module SimpleForm
mattr_accessor :label_text
@@label_text = lambda { |label, required| "#{required} #{label}" }
# Whether attributes are required by default (or not).
mattr_accessor :required_by_default
@@required_by_default = true
# Collection of methods to detect if a file type was given.
mattr_accessor :file_methods
@@file_methods = [ :mounted_as, :file?, :public_filename ]

View file

@ -63,7 +63,7 @@ module SimpleForm
end
def attribute_required_by_default?
true
SimpleForm.required_by_default
end
def required_class

View file

@ -250,6 +250,14 @@ class FormBuilderTest < ActionView::TestCase
assert_select 'input.required#user_name'
end
test 'builder input should not be required by default when ActiveModel::Validations is not included if option is set to false' do
swap SimpleForm, :required_by_default => false do
with_form_for @user, :name
assert_select 'input.optional#user_name'
end
end
test 'builder input should allow disabling required when ActiveModel::Validations is not included' do
with_form_for @user, :name, :required => false
assert_no_select 'input.required'