mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 02:11:08 -06:00
f029d8142b
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.
28 lines
569 B
Ruby
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
|