How to display multiple error (validation) messages for one field?

2,038 views
Skip to first unread message

mcbsys

unread,
Mar 13, 2012, 6:06:15 PM3/13/12
to plataformate...@googlegroups.com
I'm using SimpleForm 2.0.1 with Twitter Bootstrap 2.0.1. I like the way this puts the field-level error messages right next to the field with the "help-inline" class. However it seems this only displays one message per field. For example, given this form (with subform):
<%= simple_form_for(@account, :html => {:class => 'form-horizontal' }) do |f| %>
  <div class="inputs">
    <%= render 'account_fields', :f => f %>

    <%= f.fields_for :users do |user_form| %>
      <%= user_form.input :email, :required => true %>
      <%= user_form.input :password, :required => true %>
      <%= user_form.input :password_confirmation, :required => true %>
    <% end %>
  </div>

  <div class="actions">
    <%= f.button :submit, "Create Account" %>
  </div>
<% end %>
If I type a correct email address but then put only one character in the password field, the highlighted password field should say:
doesn't match confirmation, is too short (minimum is 8 characters)
However it only says "doesn't match confirmation." This is especially confusing since the top of the form says "2 errors prohibited this account from being saved" but only one error is listed.

How can I get simple_form to list all validation messages on a field?

Thanks,

Mark Berry
MCB Systems

Carlos Antonio da Silva

unread,
Mar 13, 2012, 7:55:55 PM3/13/12
to plataformate...@googlegroups.com
Mark, there is an option in your SimpleForm initializer called `error_method`, that is by default set to :first. This method will be called in the errors array to create the error messages, so you may change it to :to_sentence for instance, to get all errors.

-- 
At.
Carlos Antonio

mcbsys

unread,
Mar 13, 2012, 9:43:31 PM3/13/12
to plataformate...@googlegroups.com
Thanks again, Carlos! Your prompt help here has been invaluable.

A little off topic but maybe you can help me understand a "meta" question:  when I googled this, I found that two years ago, an issue had been raised re. the need to document the error_method:  https://github.com/plataformatec/simple_form/issues/30. I see that the issue is "Closed". But it seems some info is missing:  Who closed it? When was it closed? Does "Closed" mean "Resolved"? I still couldn't find any documentation on error_method in readme.md, although I see that you described it here in the group on 10/14/11. I'm glad to submit occasional documentation feedback but I'm new to the whole fork/pull thing--it sounds like a lot of work just to add a sentence to the readme.

Regards,

Mark Berry

Carlos Antonio da Silva

unread,
Mar 13, 2012, 11:07:01 PM3/13/12
to plataformate...@googlegroups.com
Seems you've found an old issue huh? I have no idea why that issue does not have the name of the one who closed it, perhaps old github times? dunno.

So yeah, I think these docs have changed a lot since then, but really there is no mystery around :error_method. It just calls send on the errors, which is actually an Array that contains both attribute/association errors (a way to show the right errors if you validate an association instead of its _id column): https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/components/errors.rb#L15

This means you can call any array method on that, we just recommend using :first and :to_sentence, that make more sense.

Anyway, please feel free to send us a pull request improving the docs, these are always welcome :). Remember you can use the edit button on github nowadays, so you don't even need to touch your editor to send such PRs.

Cheers.

-- 
At.
Carlos Antonio

mcbsys

unread,
Mar 15, 2012, 7:04:48 PM3/15/12
to plataformate...@googlegroups.com
Well I started to fork and edit the config file. I was going to change the description of :error_method to read

  # Method used to tidy up errors. Specify any Rails Array method.
  # Default :first lists first message for each field.
  # Use :to_sentence to list all errors for each field.

But now I'm confused. Do I want edit this

https://github.com/plataformatec/simple_form/blob/master/lib/simple_form.rb#L20

or this

https://github.com/plataformatec/simple_form/blob/master/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt#L104

or both? I'm guessing #2 is the one that appears in my app?

Mark Berry
MCB Systems

Carlos Antonio da Silva

unread,
Mar 15, 2012, 7:25:17 PM3/15/12
to plataformate...@googlegroups.com
Exactly, the second one inside the generators dir is copied to your app when you run the install generator, you'd be better changing there :). Thanks.

-- 
At.
Carlos Antonio

mcbsys

unread,
Mar 16, 2012, 6:45:49 PM3/16/12
to plataformate...@googlegroups.com
Alright, my first ever pull request! https://github.com/plataformatec/simple_form/pull/519

Carlos Antonio da Silva

unread,
Mar 16, 2012, 9:05:01 PM3/16/12
to plataformate...@googlegroups.com
Great, already applied :). Thanks!

-- 
At.
Carlos Antonio

mcbsys

unread,
Mar 19, 2012, 3:10:32 PM3/19/12
to plataformate...@googlegroups.com
Huh, I see that forking one file copied the entire project into my Github account. I assume that has to stay there until you pull the change back? And now that you have, can/should I delete the project from my account? If I need to make another change, I would fork again anyway, right?

Thanks,

Mark Berry

Carlos Antonio da Silva

unread,
Mar 19, 2012, 6:29:45 PM3/19/12
to plataformate...@googlegroups.com
Yup, it forks the entire project :). And yeah, you can remove it if you want, and fork again if you need to do any other change. Although it won't make any harm to your account if you let it there, up to you :)

-- 
At.
Carlos Antonio

mcbsys

unread,
Mar 19, 2012, 9:44:32 PM3/19/12
to plataformate...@googlegroups.com
Great, thanks. I can't see much point in keeping a stale version around so I'll delete my fork.
Reply all
Reply to author
Forward
0 new messages