rails validate_format_of非负整数

时间:2010-04-01 06:09:06

标签: ruby-on-rails activerecord

我正在尝试使用以下

验证非负整数的格式
validates_format_of :fundays, :with => /\A[\d]+\Z/, :message => "invalid fundays"

这是视图中使用的表单字段

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

但是,当我在此字段中输入非数字并提交表单时,验证不会失败。相反,它在数据库中保存值0。如何将其写入错误消息列表。

感谢

1 个答案:

答案 0 :(得分:7)

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