Open bayburin opened 3 years ago
This works for me
require 'test_helper'
class ValidateCollectionTest < MiniTest::Spec
class SongForm < TestForm
property :title
end
class AlbumForm < TestForm
collection :songs, form: SongForm
validation do
option :form
params do
optional(:songs)
end
rule(:songs).each do |index:|
key([:title, index]).failure('Invalid') if value[:title] == "Wrong"
end
end
end
let(:song) { Song.new("Broken") }
let(:song_with_composer) { Song.new("Resist Stance", nil, composer) }
let(:composer) { Artist.new("Greg Graffin") }
let(:artist) { Artist.new("Bad Religion") }
let(:album) { Album.new("The Dissent Of Man", [song, song_with_composer], artist) }
let(:form) { AlbumForm.new(album) }
it do
form.validate("songs" => [{"title" => "Wrong"}, {"title" => "Brok"}])
_(form.errors.messages).must_equal({:title=>{0=>["Invalid"]}})
end
end
Try replacing next if value[:id]
with next if form.model.id
as id
isn't present in your params
.
Complete Description of Issue
Hi! I'm using reform 2.5.0 with dry-validations 1.6 into rails 6.0.3 and seeing issue. I'm trying to validate a nested collection of forms from the parent form and output errors for each collection item, but
form.errors.messages
method returns empty hash.Steps to reproduce
Expected behavior
I did this validation according to dry-rb documentation: https://dry-rb.org/gems/dry-validation/1.6/rules/#defining-a-rule-for-each-element-of-an-array
I expect to see something like this:
Actual behavior
But now I see an empty hash
Although if you look at the error object, you can see that the validation worked successfully and detected an error into collection:
If I transfer the validation to the WorkflowForm and refuse to iterate the array, there is no such problem, the error is successfully displayed. But I cannot transfer validation to a nested form as I need to compare the data from
WorkForm
with data fromWorkflowForm
form.System configuration
Reform version: 2.5.0 Rails version: 6.0.3 dry-validation version: 1.6.0