Skip to content
This repository has been archived by the owner on Aug 16, 2019. It is now read-only.

Added conditional validation example #90

Open
wants to merge 1 commit into
base: f6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gems/reform/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ The model won't be touched, its values are still the original ones.

model.artist.name #=> "Duran Duran"

You can find more options and validation examples [here](/gems/reform/validation.html).

### Deserialization and Populator

Very often, you need to give Reform some information how to create or find nested objects when `validate`ing. This directive is called _populator_ and [documented here](http://trailblazer.to/gems/reform/populator.html).
Expand Down
27 changes: 27 additions & 0 deletions gems/reform/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,33 @@ At any time you can extend an existing group using `:inherit`.

This appends validations to the existing `:email` group.

## Conditional Validation

Sometimes you need to run validations based on data of your form. This can be easily done with conditional validations.

require "reform/form/dry"

class Fund::CreateCallForm < Reform::Form
feature Reform::Form::Dry

property :type
property :fund

validation do
required(:type) { filled? & included_in?(TRANSACTION_TYPES['Fund::Call']) }
end

validation if: -> (results) { rebalance? } do
required(:fund).filled
end

def rebalance?
self.type == 'rebalance'
end
end

Our form has two properties `type` and `fund` and fund is required only if type is `type == 'rebalance'`.

## Dry-validation

Dry-validation is the preferred backend for defining and executing validations.
Expand Down