diff --git a/lib/reform/form/active_model/validations.rb b/lib/reform/form/active_model/validations.rb index fea6c99..2ce24e0 100644 --- a/lib/reform/form/active_model/validations.rb +++ b/lib/reform/form/active_model/validations.rb @@ -177,7 +177,10 @@ def full_messages private def full_messages_for_nested_fields(form_fields) - form_fields.map { |field| full_messages_for_twin(field[1]) } + form_fields + .to_a + .reject { |field| field[0] == "parent" } + .map { |field| full_messages_for_twin(field[1]) } end def full_messages_for_twin(object) diff --git a/test/parent_test.rb b/test/parent_test.rb new file mode 100644 index 0000000..f0e06aa --- /dev/null +++ b/test/parent_test.rb @@ -0,0 +1,43 @@ +require 'test_helper' +require 'disposable/twin/parent' + +class ParentTest < BaseTest + + class AlbumForm < Reform::Form + + feature Disposable::Twin::Parent + + property :band + validates :band, presence: true + + collection :songs, virtual: true, default: [], populator: :populate_songs! do + property :name + validate :unique_name + + def unique_name + if name == parent.band + errors.add(:name, "Song name shouldn't be the same as #{parent.band}") + end + end + end + + def populate_songs!(fragment:, **) + existed_song = songs.find { |song| song.name == fragment[:name] } + return existed_song if existed_song + songs.append(OpenStruct.new(name: fragment[:name])) + end + + end + + let (:form) { + AlbumForm.new(OpenStruct.new( + :band => "Killer Queen" + )) + } + + it "allows nested collection validation messages to be shown" do + form.validate(songs: [{ name: "Killer Queen" }]) + _(form.errors.full_messages).must_equal(["Name Song name shouldn't be the same as Killer Queen"]) + end + +end