Skip to content

Instantly share code, notes, and snippets.

@dnd
Created November 29, 2016 20:31
Show Gist options
  • Save dnd/7a89a649c6a0ee86221ad6b019308a62 to your computer and use it in GitHub Desktop.
Save dnd/7a89a649c6a0ee86221ad6b019308a62 to your computer and use it in GitHub Desktop.
validation(with: {form: true}) do
configure do
def non_destroyed_items?(value)
form.items.detect {|i| !i._destroy}
end
def item_sum?(value)
isum = form.items.reduce(0) do |sum, item|
sum += item._destroy ? 0 : item.amount
end
isum == value
end
end
required(:date).filled(:date?)
required(:amount).filled(:int?, :item_sum?, not_eql?: 0)
required(:items).value(type?: Array, non_destroyed_items?: true) do
each do
schema do
optional(:_destroy).maybe(:bool?)
optional(:id).maybe(:int?)
required(:bucket).filled
required(:amount).filled(:int?)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment