views:

628

answers:

2

I have a model that uses the low level validate_on_create method, based on a simple condition I'm adding an error message to the field, e.g

self.errors.add(:expiry_date, "is too soon") if self.expiry_date && self.expiry_date < Date.today + 1.week

When rendering the view 'new' after a failed save the field isn't picked up by error_messages_for as I would expect. The error is shown in the ususal list of errors that indicated the validation is working correctly.

Has anybody got any idea why this is? I guess form.error_messages isn't looking at all errors on the object?

Added the requested code:

<% form_for([:solicitor, @policy]) do |form| %>
  <%= form.error_messages :message => "See the list of reasons below to find out why not:", :header_message => "Sorry but we cannot yet create your policy  based on the information you've provided." %>
  <%= render :partial => 'form', :locals => {:form => form} %>
<% end %>
+2  A: 

More than likely your problem is:

  1. You are not passing the form_for the correct options.
  2. You are redirecting back to the form instead of rendering.
  3. You aren't using the form_for builders (ie text_field_tag instead of f.text_field)

However, without posting your full controller/view, I'm basically just guessing.

BJ Clark
Thanks for this answer and sorry I don't post up enough for you to really see the problem.
tsdbrown
A: 

The problem was a simple typo in the form partial. I should have posted all the code in question as I'm sure if I did BJ Clark or others would have easily spotted the silly mistake I made

Lesson learned!

tsdbrown