diff options
author | Mayra Cabrera <mcabrera@gitlab.com> | 2019-01-24 11:59:38 -0600 |
---|---|---|
committer | Mayra Cabrera <mcabrera@gitlab.com> | 2019-01-25 16:08:21 -0600 |
commit | e1122fcc72c331f1c474d17fa6e5393327b9ea16 (patch) | |
tree | acf1144b43a168a6b851deda48fb642f71e94157 | |
parent | fac725c9e54bead036ca1ad5e2da0b3441aecbdf (diff) | |
download | gitlab-ce-56750-extend-errors-form-helper.tar.gz |
Add a new form error helper method56750-extend-errors-form-helper
This method will only display the specific error for the base model and
it will not include the errors on the relationship
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/56750
-rw-r--r-- | app/helpers/form_helper.rb | 46 | ||||
-rw-r--r-- | app/views/clusters/clusters/_integration_form.html.haml | 2 | ||||
-rw-r--r-- | app/views/clusters/clusters/gcp/_show.html.haml | 2 | ||||
-rw-r--r-- | app/views/clusters/clusters/user/_show.html.haml | 2 | ||||
-rw-r--r-- | changelogs/unreleased/56750-extend-errors-form-helper.yml | 5 | ||||
-rw-r--r-- | spec/helpers/form_helper_spec.rb | 21 |
6 files changed, 65 insertions, 13 deletions
diff --git a/app/helpers/form_helper.rb b/app/helpers/form_helper.rb index 5705ee54cee..4c1110bfef6 100644 --- a/app/helpers/form_helper.rb +++ b/app/helpers/form_helper.rb @@ -4,18 +4,17 @@ module FormHelper def form_errors(model, type: 'form') return unless model.errors.any? - pluralized = 'error'.pluralize(model.errors.count) - headline = "The #{type} contains the following #{pluralized}:" + render_form_errors(model.errors.full_messages, type) + end - 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 - end + def base_form_errors(model) + return unless invalid_attributes(model).any? + + error_messages = invalid_attributes(model).flat_map do |attribute| + model.errors.full_messages_for(attribute.to_sym) end + + render_form_errors(error_messages.flatten, 'form') end def issue_assignees_dropdown_options @@ -41,4 +40,31 @@ module FormHelper } } end + + private + + def render_form_errors(full_messages, type) + pluralized = 'error'.pluralize(full_messages.count) + headline = "The #{type} contains the following #{pluralized}:" + + error_explanation_div(headline, full_messages) + end + + def error_explanation_div(headline, error_messages) + content_tag(:div, class: 'alert alert-danger', id: 'error_explanation') do + content_tag(:h4, headline) << + content_tag(:ul) do + error_messages + .map { |msg| content_tag(:li, msg) } + .join + .html_safe + end + end + end + + def invalid_attributes(model) + return [] unless model.errors.any? + + model.attribute_names.select { |attribute| model.errors[attribute].present? } + end end diff --git a/app/views/clusters/clusters/_integration_form.html.haml b/app/views/clusters/clusters/_integration_form.html.haml index 4c47e11927e..9cca80b7dbb 100644 --- a/app/views/clusters/clusters/_integration_form.html.haml +++ b/app/views/clusters/clusters/_integration_form.html.haml @@ -1,5 +1,5 @@ = form_for @cluster, url: clusterable.cluster_path(@cluster), as: :cluster do |field| - = form_errors(@cluster) + = base_form_errors(@cluster) .form-group %h5= s_('ClusterIntegration|Integration status') %label.append-bottom-0.js-cluster-enable-toggle-area diff --git a/app/views/clusters/clusters/gcp/_show.html.haml b/app/views/clusters/clusters/gcp/_show.html.haml index e9f05eaf453..4ec27519325 100644 --- a/app/views/clusters/clusters/gcp/_show.html.haml +++ b/app/views/clusters/clusters/gcp/_show.html.haml @@ -7,7 +7,7 @@ = clipboard_button(text: @cluster.name, title: s_('ClusterIntegration|Copy Kubernetes cluster name'), class: 'input-group-text btn-default') = form_for @cluster, url: clusterable.cluster_path(@cluster), as: :cluster do |field| - = form_errors(@cluster) + = base_form_errors(@cluster) = field.fields_for :platform_kubernetes, @cluster.platform_kubernetes do |platform_kubernetes_field| .form-group diff --git a/app/views/clusters/clusters/user/_show.html.haml b/app/views/clusters/clusters/user/_show.html.haml index cac8e72edd3..e424573f01c 100644 --- a/app/views/clusters/clusters/user/_show.html.haml +++ b/app/views/clusters/clusters/user/_show.html.haml @@ -1,5 +1,5 @@ = form_for @cluster, url: clusterable.cluster_path(@cluster), as: :cluster do |field| - = form_errors(@cluster) + = base_form_errors(@cluster.platform_kubernetes) .form-group = field.label :name, s_('ClusterIntegration|Kubernetes cluster name'), class: 'label-bold' = field.text_field :name, class: 'form-control', placeholder: s_('ClusterIntegration|Kubernetes cluster name') diff --git a/changelogs/unreleased/56750-extend-errors-form-helper.yml b/changelogs/unreleased/56750-extend-errors-form-helper.yml new file mode 100644 index 00000000000..26437389da1 --- /dev/null +++ b/changelogs/unreleased/56750-extend-errors-form-helper.yml @@ -0,0 +1,5 @@ +--- +title: Removes duplicated error message on cluster page +merge_request: 24643 +author: +type: fixed diff --git a/spec/helpers/form_helper_spec.rb b/spec/helpers/form_helper_spec.rb index 18cf0031d5f..f8ab3aae129 100644 --- a/spec/helpers/form_helper_spec.rb +++ b/spec/helpers/form_helper_spec.rb @@ -43,4 +43,25 @@ describe FormHelper do end end end + + describe '#base_form_errors' do + let(:cluster) { create(:cluster, :provided_by_user, :project) } + let(:platform_kubernetes) { cluster.platform_kubernetes } + + before do + cluster.domain = 'invalid-domain' + platform_kubernetes.api_url = 'invalid-api' + + cluster.valid? + end + + it 'display errors for the particular model' do + errors = helper.base_form_errors(cluster) + + aggregate_failures do + expect(errors).to include('<li>Domain is not a fully qualified domain name</li>') + expect(errors).not_to include('<li>Platform kubernetes api url is blocked: Only allowed protocols are http, https</li>') + end + end + end end |