type cast value before blank check for invalid values

This commit is contained in:
Adam Meehan 2010-08-03 15:04:59 +10:00
parent 34a2d4b558
commit 40e404681e
3 changed files with 18 additions and 2 deletions

View File

@ -10,6 +10,8 @@ module ValidatesTimeliness
when :datetime when :datetime
value.to_time.in_time_zone value.to_time.in_time_zone
end end
rescue
nil
end end
def dummy_time(value) def dummy_time(value)

View File

@ -35,10 +35,10 @@ module ValidatesTimeliness
raw_value = attribute_raw_value(record, attr_name) || value raw_value = attribute_raw_value(record, attr_name) || value
return if (@allow_nil && raw_value.nil?) || (@allow_blank && raw_value.blank?) return if (@allow_nil && raw_value.nil?) || (@allow_blank && raw_value.blank?)
return record.errors.add(attr_name, :"invalid_#{@type}") if value.blank?
value = type_cast(value) value = type_cast(value)
return record.errors.add(attr_name, :"invalid_#{@type}") if value.blank?
@check_restrictions.each do |restriction| @check_restrictions.each do |restriction|
begin begin
restriction_value = type_cast(evaluate_option_value(options[restriction], record)) restriction_value = type_cast(evaluate_option_value(options[restriction], record))

View File

@ -26,6 +26,20 @@ describe ValidatesTimeliness::Validator do
end end
end end
it 'should not be valid for value which not valid date or time value' do
Person.validates_date :birth_date
invalid!(:birth_date, "Not a date", 'is not a valid date')
end
it 'should not be valid attribute is type cast to nil but raw value is non-nil invalid value' do
Person.validates_date :birth_date, :allow_nil => true
record = Person.new
record.stub!(:birth_date).and_return(nil)
record.stub!(:birth_date_before_type_cast).and_return("Not a date")
record.should_not be_valid
record.errors[:birth_date].first.should == 'is not a valid date'
end
describe ":allow_nil option" do describe ":allow_nil option" do
it 'should not allow nil by default' do it 'should not allow nil by default' do
Person.validates_date :birth_date Person.validates_date :birth_date