diff --git a/app/models/champs/siret_champ.rb b/app/models/champs/siret_champ.rb index d968c85b7..5f9656385 100644 --- a/app/models/champs/siret_champ.rb +++ b/app/models/champs/siret_champ.rb @@ -2,4 +2,8 @@ class Champs::SiretChamp < Champ def search_terms etablissement.present? ? etablissement.search_terms : [value] end + + def mandatory_and_blank? + mandatory? && Siret.new(siret: value).invalid? + end end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index dc9382add..0ab8763f5 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -858,6 +858,33 @@ describe Dossier do end end + context "with mandatory SIRET champ" do + let(:type_de_champ) { create(:type_de_champ_siret, mandatory: true) } + let(:champ_siret) { create(:champ_siret, type_de_champ: type_de_champ) } + + before do + dossier.champs << champ_siret + end + + it 'should not have errors' do + errors = dossier.check_mandatory_champs + expect(errors).to be_empty + end + + context "and invalid SIRET" do + before do + champ_siret.update(value: "1234") + dossier.reload + end + + it 'should have errors' do + errors = dossier.check_mandatory_champs + expect(errors).not_to be_empty + expect(errors.first).to eq("Le champ #{champ_siret.libelle} doit ĂȘtre rempli.") + end + end + end + context "with champ repetition" do let(:procedure) { create(:procedure) } let(:type_de_champ_repetition) { create(:type_de_champ_repetition, mandatory: true) }