Skip to content

Validation errors on nested forms #32

@bramj

Description

@bramj

Hi @andypike,

First off: thanks a lot for your work on Rectify!

So I've been rectifying a rather big, complex form and been running into some questions.
One of those is: how do you display errors on nested forms?

Take for instance the blog example again:

class Blog < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :blog
end

class BlogForm < Rectify::Form
  attribute :title, String
  attribute :posts, Array[PostForm]
end

class PostForm < Rectify::Form
  attribute :title, String
  attribute :body, String
end

Now in the view, we want to show validation errors. Typically, this is something like this:

-# app/views/blogs/_form.html.haml
- if form.errors.any?
  .alert
    %h4
      = pluralize(form.errors.count, 'error')
      prohibited this blog from being saved:
    %ul
      - form.errors.each do |key, message|
        %li= "#{key} #{message}"

Now if we have a validation error on a PostForm, this error will not be displayed, since it's in e.g. form.post.first.errors.

It's not difficult to loop over all the errors of nested models, but if you have many nested forms this becomes tedious.
Do you think it would be nice to be able to do something like form.all_errors?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions