Skip to content

Instantly share code, notes, and snippets.

@maicher
Last active January 28, 2020 13:35
Show Gist options
  • Save maicher/733bb40fc3138356160a66ba67c95e82 to your computer and use it in GitHub Desktop.
Save maicher/733bb40fc3138356160a66ba67c95e82 to your computer and use it in GitHub Desktop.
Inputs with dry-validation v1
def new
# initialize a hash from
@inputs = {
user_name: '',
some_field: some_object.default_some_field,
something: {
a: '',
b: 'some default value'
}
}
end
def create
create_user = CreateUserTransaction.new
create_user.call(params) do |result|
result.success do |outcome|
#..
redirect...
end
result.failure do |outcome|
flash.now[:error] = outcome[:description]
@inputs = outcome[:validation] # <- params and validation errors here
render :edit
end
end
def edit
@inputs = {
user_name: user.name,
some_field: user.some_field,
something: {
a: something.a,
b: something.b
}
}
end
def update
# similar to #create
end
class CreateUserContract < Dry::Validation::Contract
params do
required(:user_name).filled
required(:some_field).filled { Types::Retail::Url.rule }
required(:something).hash do
required(:a).filled
required(:b).filled
end
end
end
<input name="user_name" type="text" value="<%= inputs[:user_name] %>" />
<input name="some_field" type="text" value="<%= inputs[:some_filed] %>" />
<h3>Something</h3>
<input name="user_name[something][a]" type="text" value="<%= inputs[:something][:a] %>" />
<input name="user_name[something][b]" type="text" value="<%= inputs[:something][:b] %>" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment