2014-01-16 27 views
1

我想有DateTimes與月份的無效日期進行驗證。我使用Mongoid 4alpha2與軌道4,並在我的模型我有一個Mongoid /輕便摩托車得到保存的月份無效日期

field :date_of_birth, type: DateTime

當我做了定期從控制器與「1988年2月30日」一DATE_OF_BIRTH「創造」時,模型被保存爲date_of_birth爲「1988/03/1」,而不是獲取DateInvalid錯誤,就像常規的DateTime.new(1988,2,30)將在rails控制檯中一樣。我不確定腳踏車或mongoid是否繞過DateTime上的Rails驗證,是否有其他人遇到過這種情況?

這裏的鐵軌登錄

Started POST "/drivers" for 127.0.0.1 at 2014-01-16 10:42:40 -0500 Processing by DriversController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"LiKx1ZToNVtNL9FAEgyLNNWW7mABy2BPKPwVVcTtXKk=", "driver"=>{"field_worker_name"=>"Field Worker", "hack_number"=>"38924", "first_name"=>"test", "middle_initial"=>"", "last_name"=>"testing", "date_of_birth"=>"1988/02/30", "gender"=>"", "nationality"=>"", "language"=>"", "street"=>"something", "apartment_number"=>"", "city"=>"something", "state"=>"NY", "zip_code"=>"02398", "cell_phone"=>"", "other_phone"=>"", "email"=>"", "dmv_number"=>"", "state_of_residence"=>"", "has_health_insurance"=>"false", "health_plan"=>"", "date_of_recertification"=>""}, "commit"=>"Add Driver"}

MOPED: 127.0.0.1:27017 COMMAND database=admin command={:ismaster=>1} runtime: 0.5280ms

MOPED: 127.0.0.1:27017 QUERY database=healthfund_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('52d59524544b38160c000000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.4150ms

MOPED: 127.0.0.1:27017 QUERY database=healthfund_development collection=drivers selector={"hack_number"=>38924} flags=[] limit=-1 skip=0 batch_size=nil fields={:_id=>1} runtime: 1.1000ms

MOPED: 127.0.0.1:27017 INSERT database=healthfund_development collection=drivers documents=[{"field_worker_name"=>"Field Worker", "hack_number"=>38924, "first_name"=>"test", "middle_initial"=>"", "last_name"=>"testing", "date_of_birth"=>1988-03-01 00:00:00 UTC, "gender"=>"", "street"=>"something", "apartment_number"=>"", "city"=>"something", "state"=>"NY", "zip_code"=>"02398", "cell_phone"=>"", "other_phone"=>"", "email"=>"", "nationality"=>"", "language"=>"", "dmv_number"=>"", "state_of_residence"=>"", "has_health_insurance"=>false, "health_plan"=>"", "date_of_recertification"=>nil, "_id"=>38924, "updated_at"=>2014-01-16 15:42:40 UTC, "created_at"=>2014-01-16 15:42:40 UTC}] flags=[] COMMAND database=healthfund_development command={:getlasterror=>1, :w=>1} runtime: 3.4590ms Redirected to http://localhost:3000/drivers/38924 Completed 302 Found in 17ms

回答

1

看起來像Mongoid離開日期解析到MongoDB。檢查它在MongoDB的外殼:

> new Date('1988/02/30') 
ISODate("1988-03-01T08:00:00Z") 

這是完全可以接受的行爲JavaScript's Date constructor

注:圖中日期被稱爲具有一個以上參數的構造,如果值低於其合理的範圍更大(例如,13爲月份值或70爲分鐘值),相鄰值將被調整。例如。 new Date(2013,13,1)相當於new Date(2014,1,1),兩者都創建日期爲2014-01-01。對於其他值也是如此:new Date(2013,2,1,0,70)相當於new Date(2013,2,1,1,10),它們都創建了一個日期2013-02-01T01:10:00

至於象MongoDB是關心,1988/02/30是1988年加2個月加30天,自1988年是閏年,二月加30年3月1日天。

無論如何,您應該使用:type => Date作爲出生日期。當然,Date具有相同的「把它像JavaScript」行爲:

class M 
    include Mongoid::Document 
    field :d, :type => Date 
end 

m = M.create(:d => '1988/02/30') 
m.d 
# Tue, 01 Mar 1988 

,這樣沒有太大的幫助。

你可以報告一個bug,看看Mongoid人對此有何想法。在此同時,如果需要更嚴格的分析,你可以自己做:

field :date_of_birth, :type => Date 

def date_of_birth=(date) 
    super(Date.parse(date)) 
end 

然後,當你說Model.create(:date_of_birth => '1988/02/30')你會得到你的異常。

相關問題