You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Forms decouple input validation from your models. They allow you to build UI and APIs that aren't coupled to your database modeling and allow you define exactly what parameters you'll accept in a declarative way.
232
+
Forms decouple input validation from your models. They allow you to build UI and APIs that aren't coupled to your database modeling and allow you to define exactly what parameters you'll accept in a declarative way.
233
233
234
234
They're built on `ActiveModel::Model`, `ActiveModel::Attributes`, and `ActiveModel::Dirty` — so you already know the API.
235
235
@@ -270,7 +270,7 @@ form.sync(model: article)
270
270
article.title # => "Updated"
271
271
```
272
272
273
-
Any params that don't match a defined form attribute are silently ignored — no need for `strong_parameters`.
273
+
Any params that don't match a defined form attribute are ignored — no need for `strong_parameters`, your form defines what parameters you will accept.
274
274
275
275
You can also pass **state** to `.build`, which is separate from the form's attributes — it's not user input, it's context. State is available as `@state` and is useful for conditional validation (e.g., only admins can publish) and prepopulating defaults from things the user doesn't control:
276
276
@@ -304,7 +304,7 @@ class NewArticleForm < Operational::Form
304
304
end
305
305
end
306
306
307
-
# Build pulls from article (automatic) + current_user + team (via on_build)
307
+
# Build pulls from article (automatic) + current_user/team (via on_build)
308
308
form =NewArticleForm.build(model: article, state: { current_user: user, team: team, author: user })
309
309
310
310
# Sync writes to article (automatic) + author (via on_sync)
@@ -349,17 +349,17 @@ Contract helpers wire forms into operations as steps. This is where Operations a
349
349
Creates a form instance and stores it in the state:
350
350
351
351
```ruby
352
+
# Simple — builds the form and pre-populates from state[:model]
352
353
step Contract::Build(contract:ArticleForm)
353
-
# state[:contract] is now an ArticleForm instance
354
354
355
-
# With a model for pre-population:
355
+
# With a custom model key — pre-populates from state[:article] instead
0 commit comments