我有一个地址表单,我想作为一个整体验证,而不是自己验证每个输入.我只能通过将line1,city,state,zip传递给自定义谓词方法来判断地址是否有效,以便它可以将它们作为一个单元进行检查.
我怎样才能做到这一点?我只看到如何验证单个字段.
最佳答案 更新 – 这适用于ActiveRecords而不是干验证gem.
请参阅本教程,http://guides.rubyonrails.org/active_record_validations.html
引用教程,
You can also create methods that verify the state of your models and add messages to the errors collection when they are invalid. You must then register these methods by using the
validate
(API) class method, passing in the symbols for the validation methods’ names.
class Invoice < ApplicationRecord
validate :discount_cannot_be_greater_than_total_value
def discount_cannot_be_greater_than_total_value
if discount > total_value
errors.add(:discount, "can't be greater than total value")
end
end
end