diff --git a/app/models/dialog.rb b/app/models/dialog.rb index 46745d94017..989085f4122 100644 --- a/app/models/dialog.rb +++ b/app/models/dialog.rb @@ -61,6 +61,11 @@ def validate_children errors.add(:base, _("Dialog %{dialog_label} must have at least one Tab") % {:dialog_label => label}) end + duplicate_field_names = dialog_fields.collect(&:name).duplicates + if duplicate_field_names.present? + errors.add(:base, _("Dialog field name cannot be duplicated on a dialog: %{duplicates}") % {:duplicates => duplicate_field_names.join(', ')}) + end + dialog_tabs.each do |dt| next if dt.valid? dt.errors.full_messages.each do |err_msg| diff --git a/spec/models/dialog_spec.rb b/spec/models/dialog_spec.rb index 14453a004cc..e1b80e5f91a 100644 --- a/spec/models/dialog_spec.rb +++ b/spec/models/dialog_spec.rb @@ -396,6 +396,27 @@ expect { dialog.save! }.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Dialog #{dialog.label} must have at least one Tab") end + context "unique field names" do + before do + dialog.dialog_tabs << FactoryGirl.create(:dialog_tab, :label => 'tab') + dialog.dialog_tabs.first.dialog_groups << FactoryGirl.create(:dialog_group, :label => 'group') + dialog.dialog_tabs.first.dialog_groups.first.dialog_fields << FactoryGirl.create(:dialog_field, :label => 'field 1', :name => 'field1') + end + + it "fails with two identical field names on different groups" do + dialog.dialog_tabs.first.dialog_groups << FactoryGirl.create(:dialog_group, :label => 'group2') + dialog.dialog_tabs.first.dialog_groups.last.dialog_fields << FactoryGirl.create(:dialog_field, :label => 'field 3', :name => 'field1') + expect { dialog.save! } + .to raise_error(ActiveRecord::RecordInvalid, /Dialog field name cannot be duplicated on a dialog: field1/) + end + + it "fails with two identical field names on same group" do + dialog.dialog_tabs.first.dialog_groups.first.dialog_fields << FactoryGirl.create(:dialog_field, :label => 'field 3', :name => 'field1') + expect { dialog.save! } + .to raise_error(ActiveRecord::RecordInvalid, /Dialog field name cannot be duplicated on a dialog: field1/) + end + end + it "validates with tab" do dialog.dialog_tabs << FactoryGirl.create(:dialog_tab, :label => 'tab') expect_any_instance_of(DialogTab).to receive(:valid?)