mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
fix validator for :format option
This commit is contained in:
parent
29057c41f7
commit
0df3886804
@ -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?
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user