affiche le message d'erreur entré par l'administrateur

This commit is contained in:
Kara Diaby 2023-10-19 23:22:22 +00:00
parent 3c172e1115
commit 60fcb51533
4 changed files with 7 additions and 5 deletions

View file

@ -2,7 +2,7 @@ class ExpressionReguliereValidator < ActiveModel::Validator
def validate(record) def validate(record)
if record.value.present? if record.value.present?
if !record.value.match?(Regexp.new(record.expression_reguliere, timeout: 5.0)) 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
end end
rescue Regexp::TimeoutError rescue Regexp::TimeoutError

View file

@ -5,4 +5,4 @@ en:
champs/expression_reguliere_champ: champs/expression_reguliere_champ:
attributes: attributes:
value: value:
invalid_regexp: does not match expected format invalid_regexp: '%{expression_reguliere_error_message}'

View file

@ -5,4 +5,4 @@ fr:
champs/expression_reguliere_champ: champs/expression_reguliere_champ:
attributes: attributes:
value: value:
invalid_regexp: ne correspond pas au format attendu invalid_regexp: '%{expression_reguliere_error_message}'

View file

@ -1579,11 +1579,12 @@ describe Dossier, type: :model do
let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) } let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) }
let(:dossier) { create(:dossier, procedure: procedure) } let(:dossier) { create(:dossier, procedure: procedure) }
let(:types_de_champ) { [type_de_champ] } 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 context "with bad example" do
let(:expression_reguliere_exemple_text) { "01234567" } let(:expression_reguliere_exemple_text) { "01234567" }
let(:expression_reguliere) { "[A-Z]+" } let(:expression_reguliere) { "[A-Z]+" }
let(:expression_reguliere_error_message) { "Le champ doit être composé de lettres majuscules" }
before do before do
champ = dossier.champs_public.first champ = dossier.champs_public.first
@ -1593,13 +1594,14 @@ describe Dossier, type: :model do
it 'should have errors' do it 'should have errors' do
expect(dossier.errors).not_to be_empty 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
end end
context "with good example" do context "with good example" do
let(:expression_reguliere_exemple_text) { "AZERTY" } let(:expression_reguliere_exemple_text) { "AZERTY" }
let(:expression_reguliere) { "[A-Z]+" } let(:expression_reguliere) { "[A-Z]+" }
let(:expression_reguliere_error_message) { "Le champ doit être composé de lettres majuscules" }
before do before do
champ = dossier.champs_public.first champ = dossier.champs_public.first