From 8d2c5cfa14dc800491daa1e7b25d81f333f70cb0 Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Wed, 17 Jul 2019 14:59:23 +0200 Subject: [PATCH] validation par regex --- app/models/procedure.rb | 9 +++++++++ app/views/admin/procedures/_monavis.html.haml | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index dcddab930..14595964e 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -72,6 +72,15 @@ class Procedure < ApplicationRecord validates :duree_conservation_dossiers_dans_ds, allow_nil: true, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: MAX_DUREE_CONSERVATION }, unless: :durees_conservation_required validates :duree_conservation_dossiers_hors_ds, allow_nil: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, unless: :durees_conservation_required + class MonavisValidator < ActiveModel::Validator + def validate(record) + r = Regexp.new('\s*Je donne mon avis\s*', Regexp::MULTILINE) + if record.monavis.present? && !r.match?(record.monavis) + record.errors[:base] << "Le code fourni ne correspond pas au format des codes Monavis reconnus par la plateforme." + end + end + end + validates_with MonavisValidator before_save :update_juridique_required before_save :update_durees_conservation_required before_create :ensure_path_exists diff --git a/app/views/admin/procedures/_monavis.html.haml b/app/views/admin/procedures/_monavis.html.haml index aea72f433..36d631868 100644 --- a/app/views/admin/procedures/_monavis.html.haml +++ b/app/views/admin/procedures/_monavis.html.haml @@ -11,4 +11,6 @@ %p Une fois en possession du code généré sur le site MonAvis, vous pouvez le coller dans le champ ci-dessous : - = f.text_area :monavis, rows: '6', placeholder: 'Insérer le code généré par MonAvis', class: 'form-control' + .form-group + = f.label :monavis, "Mon avis" + = f.text_area :monavis, rows: '6', placeholder: 'Je donne mon avis', class: 'form-control'