可以自定義validate(), 這個方法在每次保存數據時都會被調用.
如:
def validate
if name.blank? && email.blank?
errors.add_to_base("You must specify a name or an email address")
end
end
同時也可以自定義 validate_on_create(), validate_on_update()方法.
valid?()方法可以隨時調用,用來測試數據是否能通過校驗
返回的錯誤信息可用 error_messages_for(model)方法顯示.
如:<%= error_messages_for ‘article’ %>
校驗大全:
validates_acceptance_of
指定checkbox應該選中. (如:(*)我同意條款)
用法:validates_acceptance_of attr… [ options... ]
參數:message text 默認:“must be accepted.”
:on :save, :create, or :update
實例:
class Order < ActiveRecord::Base
validates_acceptance_of :terms,
:message => "Please accept the terms to proceed"
end
validates_associated
查驗指定的object.
用法:validates_associated name… [ options... ]
參數:message text 默認: is “is invalid.”
:on :save, :create, or :update
實例:
class Order < ActiveRecord::Base
has_many :line_items
belongs_to :user
validates_associated :line_items,
:message => "are messed up"
validates_associated :user
end
validates_confirmation_of
數據重校
用法:validates_confirmation_of attr… [ options... ]
參數:message text 默認 “doesn’t match confirmation.”
:on :save, :create, or :update
實例:
對密碼錶:
<%= password_field "user", "password" %><br />
<%= password_field "user", "password_confirmation" %><br />
#第二表名為xxxx_confirmation
class User < ActiveRecord::Base
validates_confirmation_of :password
end
validates_each
使用block檢驗一個或一個以上參數.
用法:validates_each attr… [ options... ] { |model, attr, value| … }
參數:allow_nil boolean 設為true時跳過nil對象.
:on :save, :create, or :update
實例:
class User < ActiveRecord::Base
validates_each :name, :email do |model, attr, value|
if value =~ /groucho|harpo|chico/i
model.errors.add(attr,"You can’t be serious, #{value}")
end
end
end
validates_exclusion_of
確定被檢對象不包括指定數據
用法:validates_exclusion_of attr…, :in => enum [ options... ]
#enum指一切可用include?()判斷的範圍.
參數:allow_nil 設為true將直接跳過nil對象.
:in (or :within) enumerable
:message text 默認為: “is not included in the list.”
:on :save, :create, or :update
實例:
class User < ActiveRecord::Base
validates_exclusion_of :genre,
:in => %w{ polka twostep foxtrot },
:message =>"no wild music allowed"
validates_exclusion_of :age,
:in => 13..19,
:message =>"cannot be a teenager"
end
validates_inclusion_of
確認對象包括在指定範圍
用法:validates_inclusion_of attr…, :in => enum [ options... ]
參數:allow_nil 設為true直接跳過nil對象
:in (or :within) enumerable An enumerable object.
:message text 默認:“is not included in the list.”
:on :save, :create, or :update
實例:
class User < ActiveRecord::Base
validates_inclusion_of :gender,
:in => %w{ male female },
:message =>"should be ‘male’ or ‘female’"
validates_inclusion_of :age,
:in => 0..130,
:message =>"should be between 0 and 130"
end
validates_format_of
用正則檢驗對象
用法:validates_format_of attr…, :with => regexp [ options... ]
參數:message text 默認為: “is invalid.”
:on :save, :create, or :update
:with 正則表達式
實例:
class User < ActiveRecord::Base
validates_format_of :length, :with => /^d+(in|cm)/
end
validates_length_of
檢查對象長度
用法:validates_length_of attr…, [ options... ]
參數:in (or :within) range
:is integer
:minimum integer
:maximum integer
:message text 默認文字會根據參數變動,可使用%d 取代確定的最大,最小或指定數據.
:on :save, :create, or :update
:too_long text 當使用了 :maximum後的 :message
:too_short text ( :minimum )
:wrong_length ( :is)
實例:
class User < ActiveRecord::Base
validates_length_of :name, :maximum => 50
validates_length_of :password, :in => 6..20
validates_length_of :address, :minimum => 10,
:message =>"seems too short"
end
validates_numericality_of
檢驗對象是否為數值
用法:validates_numericality_of attr… [ options... ]
參數:message text 默認 “is not a number.”
:on :save, :create, or :update
:only_integer
實例:
class User < ActiveRecord::Base
validates_numericality_of :height_in_meters
validates_numericality_of :age, :only_integer => true
end
validates_presence_of
檢驗對象是否為空
用法:validates_presence_of attr… [ options... ]
參數:message text 默認:“can’t be empty.”
:on :save, :create, or :update
實例:
class User < ActiveRecord::Base
validates_presence_of :name, :address
end
validates_uniqueness_of
檢驗對象是否不重複
用法:validates_uniqueness_of attr… [ options... ]
參數:message text 默認: “has already been taken.”
:on :save, :create, or :update
:scope attr 指定範圍
實例:
class User < ActiveRecord::Base
validates_uniqueness_of :name
end
class User < ActiveRecord::Base
validates_uniqueness_of :name, :scope =>"group_id"
end
#指定在同一group_id的條件下不重複.
常用正則:
E-Mail地址格式:
validates_format_of :email,
:with => /^([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i,
:message => ‘email must be valid’
網址格式:
validates_uri_existence_of :url, :with =>
/(^$)|(^(http|https)://[a-z0-9] ([-.]{1}[a-z0-9] )*.[a-z]{2,5}(([0-9]{1,5})?/.*)?$)/ix
轉載請註明: 轉自船長日誌, 本文鏈接地址: http://www.cslog.cn/Content/ruby_on_rails_validation_helpers/zh-hant/