From 32fc1eae61473e14ffa12b8d2145459d02d72d11 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 13 Feb 2023 15:58:53 +0100 Subject: [PATCH] Remove `ship_address` errors if shipping method isn't selected --- app/controllers/split_checkout_controller.rb | 17 ++++++++++++++++- spec/system/consumer/split_checkout_spec.rb | 17 ++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/controllers/split_checkout_controller.rb b/app/controllers/split_checkout_controller.rb index 6ded33fb28..fbb7dccec8 100644 --- a/app/controllers/split_checkout_controller.rb +++ b/app/controllers/split_checkout_controller.rb @@ -49,7 +49,7 @@ class SplitCheckoutController < ::BaseController def render_error flash.now[:error] ||= I18n.t( 'split_checkout.errors.saving_failed', - messages: @order.errors.full_messages.to_sentence + messages: order_error_messages ) render status: :unprocessable_entity, operations: cable_car. @@ -57,6 +57,21 @@ class SplitCheckoutController < ::BaseController replace("#flashes", partial("shared/flashes", locals: { flashes: flash })) end + def order_error_messages + # Remove ship_address.* errors if no shipping method is not selected + remove_ship_address_errors if @order.errors[:shipping_method].present? + + end + + def remove_ship_address_errors + @order.errors.delete("ship_address.firstname") + @order.errors.delete("ship_address.address1") + @order.errors.delete("ship_address.city") + @order.errors.delete("ship_address.phone") + @order.errors.delete("ship_address.lastname") + @order.errors.delete("ship_address.zipcode") + end + def flash_error_when_no_shipping_method_available flash[:error] = I18n.t('split_checkout.errors.no_shipping_methods_available') end diff --git a/spec/system/consumer/split_checkout_spec.rb b/spec/system/consumer/split_checkout_spec.rb index 80eec57fcb..c9cb17c323 100644 --- a/spec/system/consumer/split_checkout_spec.rb +++ b/spec/system/consumer/split_checkout_spec.rb @@ -187,6 +187,19 @@ describe "As a consumer, I want to checkout my order" do click_on "Checkout as guest" end + context "should show proper list of errors" do + before do + click_button "Next - Payment method" + expect(page).to have_content "Saving failed, please update the highlighted fields." + end + + it "should not display any shipping errors messages when shipping method is not selected" do + expect(page).not_to have_content "Shipping address line 1 can't be blank" + expect(page).not_to have_content "Shipping address suburb 1 can't be blank" + expect(page).not_to have_content "Shipping address postcode can't be blank" + end + end + it "should allow visit '/checkout/details'" do expect(page).to have_current_path("/checkout/details") end @@ -306,8 +319,6 @@ describe "As a consumer, I want to checkout my order" do click_button "Next - Payment method" expect(page).to have_content "Saving failed, please update the highlighted fields." - expect(page).to have_content "Shipping address line 1 can't be blank" - expect(page).to have_content "Shipping address same as billing address?" expect(page).to have_content "Save as default shipping address" expect(page).to have_checked_field "Shipping address same as billing address?" end @@ -563,7 +574,7 @@ describe "As a consumer, I want to checkout my order" do end within ".flash[type='error']" do expect(page).to have_content("Saving failed, please update the highlighted fields") - expect(page).to have_content("can't be blank", count: 13) + expect(page).to have_content("can't be blank", count: 7) expect(page).to have_content("is invalid", count: 1) end end