discourse/lib/validators/at_least_one_group_validator.rb
Ted Johansson f029d8142b
DEV: Validate that passed in groups exist in AtLeastOneGroupValidator (#24890)
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.
2023-12-14 10:00:53 +08:00

28 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