views:

27

answers:

0

In rails forms built with form_for, all of the fields called on the form object f are wrapped in an error div if they have caused a validation failure and the form page is re-rendered.

However, if i have a form that has hand-built fields, instead of fields called on the form object f, they don't get this. Is there a way that i can trigger this on my custom fields? for example (i'm omitting the erb tags for clarity):

form_for @user do |f|
  f.text_field :first_name
  f.text_field :last_name
  text_field_tag "user[email][]", @user.email
  text_field_tag "user[email][]", ""
end

Let's assume for the sake of argument that the user can have two emails and i have a custom method in user called emails= which expect an array. That has a validation associated with it, and if it fails it adds errors to @user.errors[:email]. (Don't get hung up on the sense of this, it is just an example).

When the form re-renders, i want both the text_field_tags for email to have the fieldWithErrors div wrapped around them, ie i want to kick off ActiveRecord::Base.field_error_proc which is the method that wraps the error div around the tag.
a) how can i trigger this manually? b) is there a way anyone can see to make this happen automatically?

thanks, max