Skip to content

Commit

Permalink
reflect on allow_blank option to determine required status
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Sanghi authored and justinfrench committed Nov 26, 2010
1 parent 125dc1b commit aeaf542
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/formtastic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ def method_required?(attribute) #:nodoc:

# Determines whether the given options evaluate to true
def options_require_validation?(options) #nodoc
allow_blank = options[:allow_blank]
return !allow_blank unless allow_blank.nil?
if_condition = !options[:if].nil?
condition = if_condition ? options[:if] : options[:unless]

Expand Down
13 changes: 13 additions & 0 deletions spec/input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,19 @@ def should_be_required(options)
output_buffer.should have_tag('form li.required')
output_buffer.should_not have_tag('form li.optional')
end

it 'should not be required if allow_blank is true' do
@new_post.class.should_receive(:validators_on).with(:published).any_number_of_times.and_return([
active_model_inclusion_validator([:published], {:in => [false, true], :allow_blank => true})
])

form = semantic_form_for(@new_post) do |builder|
concat(builder.input(:published))
end
output_buffer.concat(form) if Formtastic::Util.rails3?
output_buffer.should_not have_tag('form li.required')
output_buffer.should have_tag('form li.optional')
end
end

# TODO make a matcher for this?
Expand Down

0 comments on commit aeaf542

Please sign in to comment.