Skip to content

Commit 82d8a5a

Browse files
authored
Merge pull request #2371 from jordan-brough/a-couple-address-fixes
Admin address fixes and extra address validation
2 parents 38b0d1f + 5b11430 commit 82d8a5a

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

backend/app/assets/javascripts/spree/backend/views/state_select.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ Spree.Views.StateSelect = Backbone.View.extend({
3939
},
4040

4141
render: function() {
42-
this.$state_select.empty().hide();
43-
this.$state_input.hide();
42+
this.$state_select.empty().hide().prop('disabled', true);
43+
this.$state_input.hide().prop('disabled', true);
4444

4545
if (!this.states.fetched) {
46-
this.$state_select.show().prop("disabled", true);
46+
this.$state_select.show();
4747
} else if (this.states.length) {
4848
var $state_select = this.$state_select;
4949
this.states.each(function(state) {
@@ -54,7 +54,7 @@ Spree.Views.StateSelect = Backbone.View.extend({
5454
this.$state_select.val(this.model.get("state_id"))
5555
this.$state_select.show().prop("disabled", false);
5656
} else {
57-
this.$state_input.prop('disabled', false).show();
57+
this.$state_input.show().prop('disabled', false);
5858
}
5959
}
6060
})

backend/app/views/spree/admin/shared/_address_form.html.erb

+2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@
4848
<div class="field <%= "#{type}-row" %>">
4949
<%= f.label :state_id, Spree::State.model_name.human %>
5050
<span id="<%= s_or_b %>state">
51+
<%= f.hidden_field :state_name, value: nil %>
5152
<%= f.text_field :state_name,
5253
style: "display: #{f.object.country.states.empty? ? 'block' : 'none' };",
5354
disabled: !f.object.country.states.empty?, class: 'fullwidth state_name js-state_name' %>
55+
<%= f.hidden_field :state_id, value: nil %>
5456
<%= f.collection_select :state_id, f.object.country.states.sort, :id, :name, {include_blank: true}, {class: 'custom-select fullwidth js-state_id', style: "display: #{f.object.country.states.empty? ? 'none' : 'block' };", disabled: f.object.country.states.empty?} %>
5557
</span>
5658
</div>

core/app/models/spree/address.rb

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Address < Spree::Base
1414
validates :phone, presence: true, if: :require_phone?
1515

1616
validate :state_validate
17+
validate :validate_state_matches_country
1718

1819
alias_attribute :first_name, :firstname
1920
alias_attribute :last_name, :lastname
@@ -209,5 +210,11 @@ def state_validate
209210
# ensure at least one state field is populated
210211
errors.add :state, :blank if state.blank? && state_name.blank?
211212
end
213+
214+
def validate_state_matches_country
215+
if state && state.country != country
216+
errors.add(:state, :does_not_match_country)
217+
end
218+
end
212219
end
213220
end

core/config/locales/en.yml

+4
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,10 @@ en:
639639
other: Zones
640640
errors:
641641
models:
642+
spree/address:
643+
attributes:
644+
state:
645+
does_not_match_country: does not match the country
642646
spree/calculator/tiered_flat_rate:
643647
attributes:
644648
base:

core/spec/models/spree/address_spec.rb

+18-5
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,24 @@
6868
expect(address.state_name).to be_nil
6969
end
7070

71-
it "state is entered but country does not contain that state" do
72-
address.state = state
73-
address.country = Spree::Country.new(states_required: true)
74-
address.valid?
75-
expect(address.errors["state"]).to eq(['is invalid'])
71+
context 'when the country does not match the state' do
72+
context 'when the country requires states' do
73+
it 'is invalid' do
74+
address.state = state
75+
address.country = Spree::Country.new(states_required: true)
76+
address.valid?
77+
expect(address.errors["state"]).to eq(['is invalid', 'does not match the country'])
78+
end
79+
end
80+
81+
context 'when the country does not require states' do
82+
it 'is invalid' do
83+
address.state = state
84+
address.country = Spree::Country.new(states_required: false)
85+
address.valid?
86+
expect(address.errors["state"]).to eq(['does not match the country'])
87+
end
88+
end
7689
end
7790

7891
it "both state and state_name are entered but country does not contain the state" do

0 commit comments

Comments
 (0)