Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show error for collection on correct index #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified database.sqlite3
Binary file not shown.
34 changes: 32 additions & 2 deletions test/model_validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ class ModelValidationsTest < MiniTest::Spec

class Album
include ActiveModel::Validations
attr_accessor :title, :artist, :other_attribute
attr_accessor :title, :artist, :other_attribute, :tracks

validates :title, :artist, presence: true
validates :other_attribute, presence: true
end

class Track
include ActiveModel::Validations

attr_accessor :title
end

class AlbumRating
include ActiveModel::Validations

Expand Down Expand Up @@ -40,6 +46,17 @@ class CompositeForm < Reform::Form
copy_validations_from album: Album, album_rating: AlbumRating
end

class AlbumTracksForm < Reform::Form
# property :title
# validates :title, presence: true

collection :tracks, populate_if_empty: Track do
property :title

validates :title, presence: true
end
end

let(:album) { Album.new }

describe 'non-composite form' do
Expand Down Expand Up @@ -79,4 +96,17 @@ class CompositeForm < Reform::Form

end

end
describe 'validate correct position on collection' do
let(:album) { Album.new }
let(:track1) { Track.new }
let(:track2) { Track.new }

let(:nested_form) { AlbumTracksForm.new(album) }

it 'is not valid when title is not present' do
album.tracks = [track1, track2]
nested_form.validate(artist_name: 'test', title: nil, tracks: [{title: 'Track 1'}, {title: nil}]).must_equal false
nested_form.errors[:'tracks.title'][1].must_equal ['can\'t be blank']
end
end
end