1

Validations are triggered and work as expected for attributes of the record I'm passing in (i.e required(:title).filled), but not for attributes of nested models (i.e required(:name).filled in artist).

class AlbumForm < Reform::Form
  property :title

  validation do
   required(:title).filled
  end

  property :artist do
    property :name

    validation do
     required(:name).filled
    end
  end
end

(Snippet taken from http://trailblazer.to/gems/reform)

I expect Albumform.new(album).valid? to return false if album.artist.name == nil but it does not. What am I missing here? How can this be achieved?

Using:

  • rails 4.2.7.1
  • reform-rails 0.1.7
  • reform 2.2.2
  • dry-validation 0.10.3
Lorentz Lasson
  • 885
  • 8
  • 26

1 Answers1

2

Long story short you should be using validate(params[:album]) instead of valid? http://trailblazer.to/gems/reform/#validation

def create 
  # params album: { name: nil, other_stuff: 'stuff' }

  form = AlbumForm.new(Album.new)
  if form.validate(params[:album])
    form.save
  end
end
John
  • 1,823
  • 2
  • 13
  • 14