Agile Web Development with Rails
17.4 validation
validate 在save的时候激活
validate_on_create create
validate_on_update update
通过这三个方法可以添加
validates_XXX_xxx没有的功能
错误信息写进err
#:name 是验证的属性名
def validate
unless name && name =~ /^\w+$/
errors.add(:name, "is missing or invalid" )
end
end
validates_方法有两个选项
:on 指明激活的时间 save,update,create
:message 出错时显示的消息
validate_acceptiance_of 验证复选框是否勾选
validates_acceptance_of :terms,
:message => "Please accept the terms to proceed"
validate_associated 数据库关联
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 验证变量值是否一直
在使用的时候要注意变量名,用于验证的变量名要为 XXX_confirmation
如要验证 password,相对的变量应为 password_confirmation
validates_each attr... [ options... ] { |model, attr, value| ... } 用block验证
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 attr..., :in => enum [ options... ]
验证排除在外的值,使用枚举
validates_exclusion_of :age,
:in => 13..19,
:message => "cannot be a teenager"
validates_inclusion_of attr..., :in => enum [ options... ]
验证在内的值
validates_inclusion_of :gender,
:in => %w{ male female },
:message => "should be ‘male‘ or ‘female‘"
validates_format_of attr..., :with => regexp [ options... ]
验证格式,使用正则表达式
validates_length_of
验证长度
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 验证是否是数值
:only_integer 是否是整数
validates_presence_of 验证是否为空
validates_uniqueness_of 验证是否唯一
和数据库中已有的记录比较。 scope选项限制范围
class User < ActiveRecord::Base
validates_uniqueness_of :name, :scope => "group_id"
end
---------------------------------------------------
17.5 回调
使用回调机制,可以让我们的代码参与进程监控
class User < ActiveRecord::Base
before_destroy :dont_destroy_dave
def dont_destroy_dave
raise "Can‘t destroy dave" if name == ‘dave‘
end
end
回调的点有很多
如 before_validation
after_validation
before_save
after_save
after_create
after_save
...
见332页,一共有20个回调点
使用回调有两种方式
1是定义和回调点一样名字的方法
class Order < ActiveRecord::Base
# ..
def before_save
self.payment_due ||= Time.now + 30.days
end
end
2.是定义句柄作为方法名,下面的normalize_credit_card_number(有点像filter),可同时使用多个句柄
class Order < ActiveRecord::Base
before_validation :normalize_credit_card_number
after_create do |order|
logger.info "Order #{order.id} created"
end
protected
def normalize_credit_card_number
self.cc_number.gsub!(/-\w/, ‘‘ )
end
end
回调对象
除了在model中直接使用回调,还可以定义回调对象,这样就可以跨多个model使用
class CreditCardCallbacks
# Normalize the credit card number
def before_validation(model)
model.cc_number.gsub!(/-\w/, ‘‘ )
end
end
下面是回调对象如何在两个不同的类中激活的
class Order < ActiveRecord::Base
before_validation CreditCardCallbacks.new
# ...
end
class Subscription < ActiveRecord::Base
before_validation CreditCardCallbacks.new
# ...
end
Observers
回调可能使不相关的model联系在一起,通过observer可以避免这个缺陷
通过observer使用回调的功能
OrderObserver是Order模型的程序钩子,等同于在order模型内直接使用回调
class OrderObserver < ActiveRecord::Observer
def after_save(an_order)
an_order.logger.info("Order #{an_order.id} created" )
end
end
OrderObserver.instance
observer可以用于多个model
class AuditObserver < ActiveRecord::Observer
observe Order, Payment, Refund
def after_save(model)
model.logger.info("#{model.class.name} #{model.id} created" )
end
end
AuditObserver.instance