2016-12-01 61 views
0

確認被觸發,並按照我所傳遞的記錄的屬性(即required(:title).filled)按預期工作,但不適用於嵌套模型的屬性(即required(:name).filledartist)。如何使用改革+幹驗證驗證嵌套屬性?

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 

(摘錄自http://trailblazer.to/gems/reform拍攝)

我希望Albumform.new(album).valid?到如果album.artist.name == nil但它沒有返回false。我在這裏錯過了什麼?這怎麼能實現?

使用:

  • 軌4.2.7.1
  • 改革護欄0.1.7
  • 改革2.2.2
  • 幹驗證0.10.3

回答

0

長話短說您應該使用validate(params[:album])而不是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