fix validator for :format option

This commit is contained in:
Adam Meehan 2010-09-21 20:15:26 +10:00
parent 29057c41f7
commit 0df3886804
2 changed files with 15 additions and 5 deletions

View File

@ -35,7 +35,7 @@ module ValidatesTimeliness
return if (@allow_nil && raw_value.nil?) || (@allow_blank && raw_value.blank?)
@timezone_aware = record.class.timeliness_attribute_timezone_aware?(attr_name)
value = parse(value) if value.is_a?(String)
value = parse(raw_value) if value.is_a?(String) || options[:format]
value = type_cast_value(value, @type)
return record.errors.add(attr_name, :"invalid_#{@type}") if value.blank?

View File

@ -99,18 +99,28 @@ describe ValidatesTimeliness::Validator do
end
describe ":format option" do
class PersonWithFormatOption
include TestModel
self.model_attributes = :birth_date, :birth_time, :birth_datetime
validates_date :birth_date, :format => 'dd-mm-yyyy'
end
let(:person) { PersonWithFormatOption.new }
before(:all) do
ValidatesTimeliness.use_plugin_parser = true
end
it "should be valid when value matches format" do
Person.validates_date :birth_date, :format => 'dd-mm-yyyy'
valid!(:birth_date, '11-12-1913')
person.birth_date = '11-12-1913'
person.valid?
person.errors[:birth_date].should be_empty
end
it "should not be valid when value does not match format" do
Person.validates_date :birth_date, :format => 'dd/mm/yyyy'
invalid!(:birth_date, '1913-12-11', 'is not a valid date')
person.birth_date = '1913-12-11'
person.valid?
person.errors[:birth_date].should include('is not a valid date')
end
after(:all) do