mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-02 00:05:39 +08:00
This validator is used for site settings where one or more groups are to be input. At the moment this validator just checks that the value isn't blank. This PR adds a validation for the existence of the groups passed in.
27 lines
569 B
Ruby
27 lines
569 B
Ruby
# frozen_string_literal: true
|
|
|
|
class AtLeastOneGroupValidator
|
|
def initialize(opts = {})
|
|
@opts = opts
|
|
@invalid_groups = []
|
|
end
|
|
|
|
def valid_value?(val)
|
|
@invalid_groups = []
|
|
|
|
return false if val.blank?
|
|
|
|
group_ids = val.to_s.split("|").map(&:to_i)
|
|
|
|
@invalid_groups = group_ids - Group.where(id: group_ids).pluck(:id)
|
|
@invalid_groups.empty?
|
|
end
|
|
|
|
def error_message
|
|
if @invalid_groups.empty?
|
|
I18n.t("site_settings.errors.at_least_one_group_required")
|
|
else
|
|
I18n.t("site_settings.errors.invalid_group")
|
|
end
|
|
end
|
|
end
|