2010-04-01 41 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