From 60fcb5153389c90a4884b6da67eb5126dd399fc8 Mon Sep 17 00:00:00 2001 From: Kara Diaby Date: Thu, 19 Oct 2023 23:22:22 +0000 Subject: [PATCH] =?UTF-8?q?affiche=20le=20message=20d'erreur=20entr=C3=A9?= =?UTF-8?q?=20par=20l'administrateur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/validators/expression_reguliere_validator.rb | 2 +- .../locales/models/champs/expression_reguliere_champ/en.yml | 2 +- .../locales/models/champs/expression_reguliere_champ/fr.yml | 2 +- spec/models/dossier_spec.rb | 6 ++++-- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/validators/expression_reguliere_validator.rb b/app/validators/expression_reguliere_validator.rb index 72994f767..08ed96dde 100644 --- a/app/validators/expression_reguliere_validator.rb +++ b/app/validators/expression_reguliere_validator.rb @@ -2,7 +2,7 @@ class ExpressionReguliereValidator < ActiveModel::Validator def validate(record) if record.value.present? if !record.value.match?(Regexp.new(record.expression_reguliere, timeout: 5.0)) - record.errors.add(:value, :invalid_regexp) + record.errors.add(:value, :invalid_regexp, expression_reguliere_error_message: record.expression_reguliere_error_message) end end rescue Regexp::TimeoutError diff --git a/config/locales/models/champs/expression_reguliere_champ/en.yml b/config/locales/models/champs/expression_reguliere_champ/en.yml index 29554a792..0b5ef0fc3 100644 --- a/config/locales/models/champs/expression_reguliere_champ/en.yml +++ b/config/locales/models/champs/expression_reguliere_champ/en.yml @@ -5,4 +5,4 @@ en: champs/expression_reguliere_champ: attributes: value: - invalid_regexp: does not match expected format + invalid_regexp: '%{expression_reguliere_error_message}' diff --git a/config/locales/models/champs/expression_reguliere_champ/fr.yml b/config/locales/models/champs/expression_reguliere_champ/fr.yml index a266b3f6a..ca7cc4cdc 100644 --- a/config/locales/models/champs/expression_reguliere_champ/fr.yml +++ b/config/locales/models/champs/expression_reguliere_champ/fr.yml @@ -5,4 +5,4 @@ fr: champs/expression_reguliere_champ: attributes: value: - invalid_regexp: ne correspond pas au format attendu + invalid_regexp: '%{expression_reguliere_error_message}' diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index e6a332d6c..86b461907 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1579,11 +1579,12 @@ describe Dossier, type: :model do let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) } let(:dossier) { create(:dossier, procedure: procedure) } let(:types_de_champ) { [type_de_champ] } - let(:type_de_champ) { { type: :expression_reguliere, expression_reguliere:, expression_reguliere_exemple_text: } } + let(:type_de_champ) { { type: :expression_reguliere, expression_reguliere:, expression_reguliere_exemple_text:, expression_reguliere_error_message: } } context "with bad example" do let(:expression_reguliere_exemple_text) { "01234567" } let(:expression_reguliere) { "[A-Z]+" } + let(:expression_reguliere_error_message) { "Le champ doit être composé de lettres majuscules" } before do champ = dossier.champs_public.first @@ -1593,13 +1594,14 @@ describe Dossier, type: :model do it 'should have errors' do expect(dossier.errors).not_to be_empty - expect(dossier.errors.full_messages.join(',')).to include("ne correspond pas au format attendu") + expect(dossier.errors.full_messages.join(',')).to include(dossier.champs_public.first.expression_reguliere_error_message) end end context "with good example" do let(:expression_reguliere_exemple_text) { "AZERTY" } let(:expression_reguliere) { "[A-Z]+" } + let(:expression_reguliere_error_message) { "Le champ doit être composé de lettres majuscules" } before do champ = dossier.champs_public.first