Merge pull request #4692 from consul/globalize_length

Simplify displaying debate length validation error
This commit is contained in:
Javi Martín
2021-10-06 16:49:09 +02:00
committed by GitHub
3 changed files with 18 additions and 7 deletions

View File

@@ -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 %>
<div class="form-error is-visible html-area ">
<%= @debate.errors[:description][0] %>
</div>
<% end %>
</div>
<% end %>
</fieldset>

View File

@@ -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

View File

@@ -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)