[英]How do I validate nested attributes using reform + dry-validation?
触发验证并按预期对我传递的记录的属性(例如, required(:title).filled
) required(:title).filled
,但对于嵌套模型的属性(即, required(:name).filled
artist
) required(:name).filled
。
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,但不是。 我在这里想念什么? 如何做到这一点?
使用方法:
长话短说,您应该使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.