Merge pull request #10757 from mfo/US/fix-required-dossier-link-with-missing-dossier

correct: ETQ administrateur, je souhaite que les types de champ liens vers un dossier pointent vers un dossier valide si ils sont obligatoire
This commit is contained in:
Colin Darie 2024-09-09 07:28:48 +00:00 committed by GitHub
commit 977e47fa74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 54 additions and 6 deletions

View file

@ -1,8 +1,4 @@
= @form.text_field(:value, input_opts(id: @champ.input_id, aria: { describedby: @champ.describedby_id }, inputmode: :numeric, min: 1, pattern: "[0-9]{1,12}", autocomplete: 'off', required: @champ.required?, class: "width-33-desktop #{@champ.blank? ? '' : 'small-margin'}")) = @form.text_field(:value, input_opts(id: @champ.input_id, aria: { describedby: @champ.describedby_id }, inputmode: :numeric, min: 1, pattern: "[0-9]{1,12}", autocomplete: 'off', required: @champ.required?, class: "width-33-desktop #{@champ.blank? ? '' : 'small-margin'}"))
- if !@champ.blank? - if !@champ.blank? && !dossier.blank?
- if dossier.blank?
.fr-error-text.fr-mb-4w
= t('.not_found')
- else
.fr-info-text.fr-mb-4w= sanitize(dossier.text_summary) .fr-info-text.fr-mb-4w= sanitize(dossier.text_summary)

View file

@ -2,9 +2,16 @@
class Champs::DossierLinkChamp < Champ class Champs::DossierLinkChamp < Champ
validate :value_integerable, if: -> { value.present? }, on: :prefill validate :value_integerable, if: -> { value.present? }, on: :prefill
validate :dossier_exists, if: -> { validate_champ_value? && !value.nil? }
private private
def dossier_exists
if mandatory? && !Dossier.exists?(value)
errors.add(:value, :not_found)
end
end
def value_integerable def value_integerable
Integer(value) Integer(value)
rescue ArgumentError rescue ArgumentError

View file

@ -4,3 +4,10 @@ en:
champs/dossier_link_champ: champs/dossier_link_champ:
hints: hints:
value: "File number" value: "File number"
errors:
models:
champs/dossier_link_champ:
attributes:
value:
not_found: "File not found"

View file

@ -4,3 +4,11 @@ fr:
champs/dossier_link_champ: champs/dossier_link_champ:
hints: hints:
value: "Numéro de dossier" value: "Numéro de dossier"
errors:
models:
champs/dossier_link_champ:
attributes:
value:
not_found: "Le dossier n'existe pas"

View file

@ -36,4 +36,34 @@ describe Champs::DossierLinkChamp, type: :model do
end end
end end
end end
describe 'validation' do
let(:champ) { Champs::DossierLinkChamp.new(value:, dossier: build(:dossier)) }
before do
allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_dossier_link, mandatory:))
champ.run_callbacks(:validation)
end
subject { champ.validate(:champs_public_value) }
context 'when not mandatory' do
let(:mandatory) { false }
let(:value) { nil }
it { is_expected.to be_truthy }
end
context 'when mandatory' do
let(:mandatory) { true }
context 'when valid id' do
let(:value) { create(:dossier).id }
it { is_expected.to be_truthy }
end
context 'when invalid id' do
let(:value) { 'kthxbye' }
it { is_expected.to be_falsey }
end
end
end
end end