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:
Carlos Antonio da Silva 2010-09-26 23:53:06 -03:00
parent 6764fc6ee1
commit 583bdaf2b7
4 changed files with 26 additions and 4 deletions

View File

@ -30,7 +30,9 @@ module SimpleForm
end
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
def input_html_classes

View File

@ -5,6 +5,16 @@ module SimpleForm
@builder.hidden_field(attribute_name, input_html_options)
end
alias :input :render
protected
def attribute_required?
false
end
def required_class
nil
end
end
end
end

View File

@ -247,7 +247,7 @@ class FormBuilderTest < ActionView::TestCase
# REQUIRED AND PRESENCE VALIDATION
test 'builder input should obtain required from ActiveModel::Validations when it is included' do
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
assert_select 'input.optional#validating_user_status'
end
@ -256,24 +256,26 @@ class FormBuilderTest < ActionView::TestCase
with_form_for @validating_user, :name, :required => false
assert_select 'input.optional#validating_user_name'
with_form_for @validating_user, :status, :required => true
assert_select 'input.required#validating_user_status'
assert_select 'input.required[required]#validating_user_status'
end
test 'builder input should be required by default when ActiveModel::Validations is not included' do
with_form_for @user, :name
assert_select 'input.required#user_name'
assert_select 'input.required[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'
assert_no_select 'input[required]'
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'
assert_no_select 'input[required]'
assert_select 'input.optional#user_name'
end

View File

@ -184,6 +184,14 @@ class InputTest < ActionView::TestCase
assert_no_select 'label'
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
test 'input should generate a country select field' do
with_input_for @user, :country, :country