remove setting attribute to nil in validations when invalid to preserve before_type_cast value

This commit is contained in:
Adam Meehan 2008-07-30 12:22:06 +10:00
parent 037c3ea5c5
commit 8cad1b880e
2 changed files with 8 additions and 6 deletions

View File

@ -72,14 +72,12 @@ module ValidatesTimeliness
column = record.column_for_attribute(attr_name)
begin
unless time = parse_date_time(raw_value, configuration[:type])
record.send("#{attr_name}=", nil)
record.errors.add(attr_name, configuration["invalid_#{configuration[:type]}_message".to_sym])
next
end
validate_timeliness_restrictions(record, attr_name, time, configuration)
rescue Exception => e
record.send("#{attr_name}=", nil)
record.errors.add(attr_name, configuration["invalid_#{configuration[:type]}_message".to_sym])
end
end

View File

@ -83,16 +83,20 @@ describe ValidatesTimeliness::Validations do
person.errors.on(:birth_time).should == "is not a valid time"
end
it "should have same value for before_type_cast after failed validation" do
person.birth_date_and_time = "2000-01-01 25:02:03"
person.should_not be_valid
person.birth_date_and_time_before_type_cast.should == "2000-01-01 25:02:03"
end
it "should be valid with valid values" do
person.birth_date_and_time = "2000-01-01 12:12:12"
person.birth_date = "2000-01-31"
person.should be_valid
end
it "should be valid with values before out of Time range" do
it "should be valid with value out of range for Time class" do
person.birth_date_and_time = "1890-01-01 12:12:12"
person.birth_date = "1890-01-31"
person.birth_time = "23:59:59"
person.should be_valid
end