2022-09-25 22:58:40 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-10-24 20:45:10 -05:00
|
|
|
class AtLeastOneGroupValidator
|
2022-09-25 22:58:40 -05:00
|
|
|
def initialize(opts = {})
|
|
|
|
@opts = opts
|
2023-12-13 20:00:53 -06:00
|
|
|
@invalid_groups = []
|
2022-09-25 22:58:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_value?(val)
|
2023-12-13 20:00:53 -06:00
|
|
|
@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?
|
2022-09-25 22:58:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
2023-12-13 20:00:53 -06:00
|
|
|
if @invalid_groups.empty?
|
|
|
|
I18n.t("site_settings.errors.at_least_one_group_required")
|
|
|
|
else
|
|
|
|
I18n.t("site_settings.errors.invalid_group")
|
|
|
|
end
|
2022-09-25 22:58:40 -05:00
|
|
|
end
|
|
|
|
end
|