2010-04-01 61 views
1

我試圖驗證非負整數的格式如下軌validate_format_of非負整數

validates_format_of :fundays, :with => /\A[\d]+\Z/, :message => "invalid fundays" 

這裏是視圖中使用

<%= f.text_field :fundays, :maxlength => 3, :style => 'width:50px;' %> 

但是表單域,當我在此字段中輸入一個非數字並提交表單時,它不會驗證失敗。相反,它會在數據庫中保存值0。如何將它寫入錯誤消息列表。

感謝

回答

7
validates_numericality_of :fundays, :only_integer =>true, 
          :greater_than_or_equal_to =>0, 
          :message => "invalid fundays" 
+0

不應上述正則表達式還工作嗎? – user290870 2010-04-01 06:33:40