2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-13 18:18:20 +01:00
|
|
|
class Champs::DossierLinkChamp < Champ
|
2023-02-22 15:56:31 +01:00
|
|
|
validate :value_integerable, if: -> { value.present? }, on: :prefill
|
2024-09-06 11:05:21 +02:00
|
|
|
validate :dossier_exists, if: -> { validate_champ_value? && !value.nil? }
|
2023-02-22 15:56:31 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-09-06 11:05:21 +02:00
|
|
|
def dossier_exists
|
|
|
|
if mandatory? && !Dossier.exists?(value)
|
|
|
|
errors.add(:value, :not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-22 15:56:31 +01:00
|
|
|
def value_integerable
|
|
|
|
Integer(value)
|
|
|
|
rescue ArgumentError
|
|
|
|
errors.add(:value, :not_integerable)
|
|
|
|
end
|
2018-02-13 18:18:20 +01:00
|
|
|
end
|