开发者

Rails: form.error_messages does not wrap custom validated fields in a fieldWithErrors div

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开发者_JAVA技巧 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 %>


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜