自定 validation与validates

validates :roles, presence: true
  #validates :innergroups, presence: true, :if => :need_roles_groups?
  validate :expiration

  def expiration
    unless self.roles.blank?
    self.roles.each do |role|
      roles = ['2','3','4','5','9']
      if roles.include? role
        if self.innergroups?
          if !self.innergroups[Role.find(role).name.to_s].present?
            errors.add(:innergroups, "不能为空 => [#{I18n.t(Role.find(role).name)}]")
          end
        else
          errors.add(:innergroups, "不能为空 => [#{I18n.t(Role.find(role).name)}]")
        end
      end
    end
    end
  end

你可能感兴趣的:(自定 validation与validates)