← Graph

Form Object as Model Concern

concept 3 connections

Lightweight Rails form-object technique Ivan Nemytchenko demonstrates: create a concern (e.g. ModerationArticleForm) that `delegate`s everything to the wrapped model so Rails treats it as the same model, then add form-specific validations and extra attributes. Used like a normal model in controllers and integrates with nested attributes. Unlike dry-rb or hanami form objects it is almost zero-maintenance, stays within Rails defaults, and lets form hierarchies mirror controller namespaces (e.g. Web::Moderation::ArticleFormController). Solves the 'conditional validations polluting models' problem: moderation-specific validations (e.g. 'publication date set') live on the form rather than as `if: :moderated?` on the model. Acknowledged as a dirty hack that is acceptable because it lets you build a good system.

category
pattern
about
Form Object as Model Concern concept
Demonstrates a concern-plus-delegators form object that plays well with Rails defaults and nested attributes.
recommends
Form Object as Model Concern concept
Recommends a minimal concern-based form-object technique that plays nicely with Rails defaults.
concept Form Object as Model Concern
related_to
Reform tool
Contrasted with heavier form-object libraries that require constructing everything from scratch.

Provenance

Read by
2 extractions