rails3中的验证,以下方法会触发验证
以下方法则会跳过验证,将数据保存到数据库中
当使用
验证也会被跳过。
上传时验证文件类型:
create
create!
save
save!
update
update_attributes
update_attributes!
以下方法则会跳过验证,将数据保存到数据库中
decrement!
decrement_counter
increment!
increment_counter
toggle!
update_all
update_attribute
update_counters
当使用
save(:validate => false)
验证也会被跳过。
validates_acceptance_of
必须接受(多用于霸王条款)
class Library < ActiveRecord::Base
has_many :books
validates_associated :books
end
validates_confirmation_of :password(多用于验证两次密码)
class Account < ActiveRecord::Base validates_exclusion_of :subdomain, :in => %w(www), :message => "Subdomain %{value} is reserved."
end (用于验证是否包含此www)
class Product < ActiveRecord::Base validates_format_of :legacy_code, :with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed"end
(格式验证)
上传时验证文件类型:
# 验证文件后缀
validates_format_of :photo, :with => %r{\.(gif|png|jpg)$}i, :message => "must be a URL for a GIF, JPG, or PNG image"
# 如果使用file_column,则可以用以下方法
validates_file_format_of :photo, :in => ["gif", "png", "jpg"]