Add required html attribute for inputs - HTML5, closes #63.
This also ensures hidden inputs does not have required/optional options.
This commit is contained in:
parent
6764fc6ee1
commit
583bdaf2b7
|
@ -30,7 +30,9 @@ module SimpleForm
|
||||||
end
|
end
|
||||||
|
|
||||||
def input_html_options
|
def input_html_options
|
||||||
html_options_for(:input, input_html_classes)
|
html_options = html_options_for(:input, input_html_classes)
|
||||||
|
html_options[:required] = true if attribute_required?
|
||||||
|
html_options
|
||||||
end
|
end
|
||||||
|
|
||||||
def input_html_classes
|
def input_html_classes
|
||||||
|
|
|
@ -5,6 +5,16 @@ module SimpleForm
|
||||||
@builder.hidden_field(attribute_name, input_html_options)
|
@builder.hidden_field(attribute_name, input_html_options)
|
||||||
end
|
end
|
||||||
alias :input :render
|
alias :input :render
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def attribute_required?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def required_class
|
||||||
|
nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -247,7 +247,7 @@ class FormBuilderTest < ActionView::TestCase
|
||||||
# REQUIRED AND PRESENCE VALIDATION
|
# REQUIRED AND PRESENCE VALIDATION
|
||||||
test 'builder input should obtain required from ActiveModel::Validations when it is included' do
|
test 'builder input should obtain required from ActiveModel::Validations when it is included' do
|
||||||
with_form_for @validating_user, :name
|
with_form_for @validating_user, :name
|
||||||
assert_select 'input.required#validating_user_name'
|
assert_select 'input.required[required]#validating_user_name'
|
||||||
with_form_for @validating_user, :status
|
with_form_for @validating_user, :status
|
||||||
assert_select 'input.optional#validating_user_status'
|
assert_select 'input.optional#validating_user_status'
|
||||||
end
|
end
|
||||||
|
@ -256,24 +256,26 @@ class FormBuilderTest < ActionView::TestCase
|
||||||
with_form_for @validating_user, :name, :required => false
|
with_form_for @validating_user, :name, :required => false
|
||||||
assert_select 'input.optional#validating_user_name'
|
assert_select 'input.optional#validating_user_name'
|
||||||
with_form_for @validating_user, :status, :required => true
|
with_form_for @validating_user, :status, :required => true
|
||||||
assert_select 'input.required#validating_user_status'
|
assert_select 'input.required[required]#validating_user_status'
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'builder input should be required by default when ActiveModel::Validations is not included' do
|
test 'builder input should be required by default when ActiveModel::Validations is not included' do
|
||||||
with_form_for @user, :name
|
with_form_for @user, :name
|
||||||
assert_select 'input.required#user_name'
|
assert_select 'input.required[required]#user_name'
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'builder input should not be required by default when ActiveModel::Validations is not included if option is set to false' do
|
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
|
swap SimpleForm, :required_by_default => false do
|
||||||
with_form_for @user, :name
|
with_form_for @user, :name
|
||||||
assert_select 'input.optional#user_name'
|
assert_select 'input.optional#user_name'
|
||||||
|
assert_no_select 'input[required]'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'builder input should allow disabling required when ActiveModel::Validations is not included' do
|
test 'builder input should allow disabling required when ActiveModel::Validations is not included' do
|
||||||
with_form_for @user, :name, :required => false
|
with_form_for @user, :name, :required => false
|
||||||
assert_no_select 'input.required'
|
assert_no_select 'input.required'
|
||||||
|
assert_no_select 'input[required]'
|
||||||
assert_select 'input.optional#user_name'
|
assert_select 'input.optional#user_name'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -184,6 +184,14 @@ class InputTest < ActionView::TestCase
|
||||||
assert_no_select 'label'
|
assert_no_select 'label'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test 'required/optional options should not be generated for hidden inputs' do
|
||||||
|
with_input_for @user, :name, :hidden
|
||||||
|
assert_no_select 'input.required'
|
||||||
|
assert_no_select 'input[required]'
|
||||||
|
assert_no_select 'input.optional'
|
||||||
|
assert_select 'input.hidden#user_name'
|
||||||
|
end
|
||||||
|
|
||||||
# PriorityInput
|
# PriorityInput
|
||||||
test 'input should generate a country select field' do
|
test 'input should generate a country select field' do
|
||||||
with_input_for @user, :country, :country
|
with_input_for @user, :country, :country
|
||||||
|
|
Loading…
Reference in New Issue