From 4631633adfe9c830568958d433872e62d5380eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sun, 5 Sep 2021 01:45:41 +0200 Subject: [PATCH] Simplify displaying debate length validation error Adding the error to the translation means Rails will automatically show the error message in the view. We're also adding a test to make sure the error message is correctly displayed. We now have duplication in the validation rules, however. Validating translatable attributes is still a bit of mess. --- app/components/debates/form_component.html.erb | 5 ----- app/models/debate.rb | 8 ++++++-- spec/system/debates_spec.rb | 12 ++++++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/components/debates/form_component.html.erb b/app/components/debates/form_component.html.erb index b469e6792..f7d1b2098 100644 --- a/app/components/debates/form_component.html.erb +++ b/app/components/debates/form_component.html.erb @@ -18,11 +18,6 @@ <%= translations_form.text_area :description, maxlength: Debate.description_max_length, class: "html-area" %> - <% if @debate.errors.present? && locale == translations_form.locale %> -
- <%= @debate.errors[:description][0] %> -
- <% end %> <% end %> diff --git a/app/models/debate.rb b/app/models/debate.rb index ce034693f..5341197f7 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -30,7 +30,7 @@ class Debate < ApplicationRecord validates_translation :title, presence: true, length: { in: 4..Debate.title_max_length } validates_translation :description, presence: true - validate :description_sanitized + validate :description_length validates :author, presence: true validates :terms_of_service, acceptance: { allow_nil: false }, on: :create @@ -164,13 +164,17 @@ class Debate < ApplicationRecord orders end - def description_sanitized + def description_length real_description_length = ActionView::Base.full_sanitizer.sanitize(description.to_s).squish.length + if real_description_length < Debate.description_min_length errors.add(:description, :too_short, count: Debate.description_min_length) + translation.errors.add(:description, :too_short, count: Debate.description_min_length) end + if real_description_length > Debate.description_max_length errors.add(:description, :too_long, count: Debate.description_max_length) + translation.errors.add(:description, :too_long, count: Debate.description_max_length) end end end diff --git a/spec/system/debates_spec.rb b/spec/system/debates_spec.rb index 956782b81..6e61163af 100644 --- a/spec/system/debates_spec.rb +++ b/spec/system/debates_spec.rb @@ -247,6 +247,18 @@ describe "Debates" do expect(page).to have_content error_message end + scenario "Short description errors" do + login_as(create(:user)) + + visit new_debate_path + fill_in_ckeditor "Initial debate text", with: "Go!" + click_button "Start a debate" + + within ".form-error.html-area" do + expect(page).to have_content "is too short" + end + end + scenario "JS injection is prevented but safe html is respected", :no_js do author = create(:user) login_as(author)