Commit d2a908db authored by Stan Hu's avatar Stan Hu

Merge branch 'tweak-wiki-title-validation-message' into 'master'

Include invalid directories in wiki title message

See merge request gitlab-org/gitlab!25376
parents 59749b56 02261356
......@@ -3,18 +3,23 @@
module FormHelper
prepend_if_ee('::EE::FormHelper') # rubocop: disable Cop/InjectEnterpriseEditionModule
def form_errors(model, type: 'form')
def form_errors(model, type: 'form', truncate: [])
return unless model.errors.any?
headline = n_('The %{type} contains the following error:', 'The %{type} contains the following errors:', model.errors.count) % { type: type }
truncate = Array.wrap(truncate)
content_tag(:div, class: 'alert alert-danger', id: 'error_explanation') do
content_tag(:h4, headline) <<
content_tag(:ul) do
model.errors.full_messages
.map { |msg| content_tag(:li, msg) }
.join
.html_safe
messages = model.errors.map do |attribute, message|
message = model.errors.full_message(attribute, message)
message = content_tag(:span, message, class: 'str-truncated-100') if truncate.include?(attribute)
content_tag(:li, message)
end
messages.join.html_safe
end
end
end
......
......@@ -333,11 +333,15 @@ class WikiPage
*dirnames, title = @attributes[:title].split('/')
if title.bytesize > MAX_TITLE_BYTES
errors.add(:title, _("exceeds the limit of %{bytes} bytes for page titles") % { bytes: MAX_TITLE_BYTES })
errors.add(:title, _("exceeds the limit of %{bytes} bytes") % { bytes: MAX_TITLE_BYTES })
end
if dirnames.any? { |d| d.bytesize > MAX_DIRECTORY_BYTES }
errors.add(:title, _("exceeds the limit of %{bytes} bytes for directory names") % { bytes: MAX_DIRECTORY_BYTES })
invalid_dirnames = dirnames.select { |d| d.bytesize > MAX_DIRECTORY_BYTES }
invalid_dirnames.each do |dirname|
errors.add(:title, _('exceeds the limit of %{bytes} bytes for directory name "%{dirname}"') % {
bytes: MAX_DIRECTORY_BYTES,
dirname: dirname
})
end
end
end
......@@ -4,7 +4,7 @@
= form_for [@project.namespace.becomes(Namespace), @project, @page], method: @page.persisted? ? :put : :post,
html: { class: form_classes },
data: { uploads_path: uploads_path } do |f|
= form_errors(@page)
= form_errors(@page, truncate: :title)
- if @page.persisted?
= f.hidden_field :last_commit_sha, value: @page.last_commit_sha
......
---
title: Include invalid directories in wiki title message
merge_request: 25376
author:
type: changed
......@@ -23245,10 +23245,10 @@ msgstr ""
msgid "estimateCommand|%{slash_command} will update the estimated time with the latest command."
msgstr ""
msgid "exceeds the limit of %{bytes} bytes for directory names"
msgid "exceeds the limit of %{bytes} bytes"
msgstr ""
msgid "exceeds the limit of %{bytes} bytes for page titles"
msgid "exceeds the limit of %{bytes} bytes for directory name \"%{dirname}\""
msgstr ""
msgid "expired on %{milestone_due_date}"
......
......@@ -39,6 +39,25 @@ describe FormHelper do
end
end
it 'renders messages truncated if requested' do
model = double(errors: errors_stub('Error 1', 'Error 2'))
model.errors.add(:title, 'is truncated')
model.errors.add(:base, 'Error 3')
expect(model.class).to receive(:human_attribute_name) do |attribute|
attribute.to_s.capitalize
end
errors = helper.form_errors(model, truncate: :title)
aggregate_failures do
expect(errors).to include('<li>Error 1</li>')
expect(errors).to include('<li>Error 2</li>')
expect(errors).to include('<li><span class="str-truncated-100">Title is truncated</span></li>')
expect(errors).to include('<li>Error 3</li>')
end
end
def errors_stub(*messages)
ActiveModel::Errors.new(double).tap do |errors|
messages.each { |msg| errors.add(:base, msg) }
......
......@@ -192,16 +192,17 @@ describe WikiPage do
expect(subject).not_to be_valid
expect(subject.errors[:title]).to contain_exactly(
"exceeds the limit of #{max_title} bytes for page titles"
"exceeds the limit of #{max_title} bytes"
)
end
it 'rejects directories exceeding the limit' do
subject.title = invalid_directory + '/foo'
subject.title = "#{invalid_directory}/#{invalid_directory}2/foo"
expect(subject).not_to be_valid
expect(subject.errors[:title]).to contain_exactly(
"exceeds the limit of #{max_directory} bytes for directory names"
"exceeds the limit of #{max_directory} bytes for directory name \"#{invalid_directory}\"",
"exceeds the limit of #{max_directory} bytes for directory name \"#{invalid_directory}2\""
)
end
......@@ -210,8 +211,8 @@ describe WikiPage do
expect(subject).not_to be_valid
expect(subject.errors[:title]).to contain_exactly(
"exceeds the limit of #{max_title} bytes for page titles",
"exceeds the limit of #{max_directory} bytes for directory names"
"exceeds the limit of #{max_title} bytes",
"exceeds the limit of #{max_directory} bytes for directory name \"#{invalid_directory}\""
)
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment