rails validate_format_of非负整数

use*_*870 1 activerecord ruby-on-rails

我试图用以下方法验证非负整数的格式

validates_format_of :fundays, :with => /\A[\d]+\Z/, :message => "invalid fundays"
Run Code Online (Sandbox Code Playgroud)

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

<%= f.text_field :fundays, :maxlength => 3, :style => 'width:50px;' %>
Run Code Online (Sandbox Code Playgroud)

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

谢谢

Sal*_*lil 7

validates_numericality_of :fundays, :only_integer =>true, 
                          :greater_than_or_equal_to =>0, 
                          :message => "invalid fundays"
Run Code Online (Sandbox Code Playgroud)