From d6ba4d05694678aa4b73e4e5b95478520b384b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Sibaud?= Date: Sun, 10 Mar 2024 13:04:50 +0100 Subject: [PATCH] Add extra validations for links, trackers; and news - bookmarks: unique title, unique URL, force lang definition - news (direct to moderation): force section definition, force lang definition on links, different messages for max length of news title and link title - news (in redaction): force lang definition on links, different messages for max length of news title and link title - trackers: for category definition --- app/models/bookmark.rb | 6 ++++-- app/models/link.rb | 8 +++++++- app/views/bookmarks/_form.html.haml | 2 +- app/views/news/_form.html.haml | 2 +- app/views/trackers/_form.html.haml | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb index 75e47a37f..1ffd60154 100644 --- a/app/models/bookmark.rb +++ b/app/models/bookmark.rb @@ -24,10 +24,12 @@ class Bookmark < Content belongs_to :owner, class_name: 'User' validates :title, presence: { message: "Le titre est obligatoire" }, - length: { maximum: 100, message: "Le titre est trop long" } + length: { maximum: 100, message: "Le titre est trop long" }, + uniqueness: { message: "Un lien avec le même titre a déjà été proposé" } validates :link, presence: { message: "Vous ne pouvez pas poster un lien vide" }, http_url: { message: "Le lien n'est pas valide" }, - length: { maximum: 255, message: "Le lien est trop long" } + length: { maximum: 255, message: "Le lien est trop long" }, + uniqueness: { message: "Le lien a déjà été proposé" } def link=(raw) raw.strip! diff --git a/app/models/link.rb b/app/models/link.rb index 52fde9b42..b2df75d5e 100644 --- a/app/models/link.rb +++ b/app/models/link.rb @@ -25,10 +25,11 @@ class Link < ActiveRecord::Base Accessible = [:id, :user, :title, :url, :lang] validates :title, presence: { message: "Un lien doit obligatoirement avoir un titre" }, - length: { maximum: 100, message: "Le titre est trop long" } + length: { maximum: 100, message: "Le titre du lien est trop long" } validates :url, http_url: { protocols: PROTOCOLS, message: "L'adresse n'est pas valide" }, presence: { message: "Un lien doit obligatoirement avoir une adresse" }, length: { maximum: 255, message: "L’adresse est trop longue" } + validate :lang_validation def url=(raw) raw.strip! @@ -44,6 +45,11 @@ def url=(raw) write_attribute :url, raw end + def lang_validation + if lang == "xx" + errors.add(:lang, "La langue du lien doit être définie") unless title.blank? or url.blank? + end + end ### Behaviour ### def self.hit(id) diff --git a/app/views/bookmarks/_form.html.haml b/app/views/bookmarks/_form.html.haml index 5c94b3a65..7d1f04daa 100644 --- a/app/views/bookmarks/_form.html.haml +++ b/app/views/bookmarks/_form.html.haml @@ -8,7 +8,7 @@ = form.text_field :link, autocomplete: 'off', required: 'required', spellcheck: 'false', maxlength: 1024 %p = form.label :lang, "Langue" - = form.select :lang, Lang.all + = form.select :lang, Lang.all, { include_blank: true }, { required: "required" } %p - if form.object.new_record? %p diff --git a/app/views/news/_form.html.haml b/app/views/news/_form.html.haml index 845e91081..4fbe36f0a 100644 --- a/app/views/news/_form.html.haml +++ b/app/views/news/_form.html.haml @@ -16,7 +16,7 @@ = form.text_field :title, autocomplete: 'off', required: 'required', spellcheck: 'true', maxlength: 100 %p = form.label :section_id, "Section de la dépêche" - = form.collection_select :section_id, Section.published, :id, :title + = form.collection_select :section_id, Section.published, :id, :title, { include_blank: true }, { required: "required" } %p = form.label :wiki_body, "Contenu de la dépêche" = form.text_area :wiki_body, required: 'required', spellcheck: 'true', class: 'markItUp' diff --git a/app/views/trackers/_form.html.haml b/app/views/trackers/_form.html.haml index aac89aba8..229ba8f26 100644 --- a/app/views/trackers/_form.html.haml +++ b/app/views/trackers/_form.html.haml @@ -5,7 +5,7 @@ = form.text_field :title, autocomplete: 'off', required: 'required', spellcheck: 'true', maxlength: 100 %p = form.label :category_id, "Catégorie" - = form.collection_select :category_id, Category.all, :id, :title + = form.collection_select :category_id, Category.all, :id, :title, { include_blank: true }, { required: "required" } - if @tracker.new_record? %p.pot_de_miel = form.label :pot_de_miel, "Ne pas remplir ce champ"