2

我正在开发 Rails 3 应用程序。

我想验证“ Cake ”模型的“ size ”属性输入字段只允许用户输入+1,-1,+10,-10+25,-25,仅此而已。

我使用以下验证来验证“大小”:

class Cake < ActiveRecord::Base
  validates_format_of :size, :with => /^[-+]?(1|10|25)$/, :message=>'size not allowed.'
  ...

end

(我的数据库“cakes”表中的“size”属性是“ double ”类型。)

在 UI 中,即使我输入 1 或 10 或 25 或 +1 或其他任何值,我总是会收到验证失败消息。为什么我的验证没有通过,即使值是正确的?

4

1 回答 1

3

我不确定使用正则表达式验证整数是否有效。

你可以试试validates_inclusion_of :size, :in=>[-1,+1,-10,+10,-25,+25], :message=>'size not allowed.'

于 2011-04-05T12:39:02.373 回答