2

I'm well aware that there are questions that ask this but I am not allowed to comment on any of them and in any case my question is a little different from those. I am trying to get a custom error message from a validation, where the custom error message does not have to begin with the attribute being validated.

My attribute is called "form_person" and represents the name of the person filling in a form (a legal requirement for the site I'm working on). So I have

validates_presence_of :form_person, message: "^We need to know who is filling in this form (your name)"

This used to work, as in the "^" would cause the message to be added to errors[:base], but now it's not, and I get "Form person ^We need to know who is filling in this form (your name)", which is... well... less than helpful. I've seen a solution from like 2 years ago that involves messing around with the localization files, but that is honestly the last place in the entire world that I or any of the rest of our staff might look for a validation message, seeing as we're not localized at all. I know I can just write a custom validator method and that's probably what I'm going to have to do, but I don't like to do that because it's messy and needlessly pollutes the model namespace with extraneous methods. There's already the validates_presence_of method! I've looked all over the rails guides including the edge one and there's no mention of any way of doing this except via custom validator or (ugh) a custom validation class separate. Just checking for the presence of an attribute hardly seems like an appropriate thing to write a whole separate validator class for, so how can I do this conveniently in one succinct line like I used to be able to?

max
  • 199
  • 1
  • 2
  • 7
  • 1
    Are you using `custom_error_message` gem? Github page says it has support for `rails 3` and `ruby 1.9`. No mention on `rails 4` – usha Feb 20 '14 at 19:02
  • Yeah that's exactly what happened (see my comment on the answer below, and Emu's answer). Seeing as it doesn't mention support for Rails 4 and this isn't something I feel like is involved enough to require a gem, I've just written a custom validator for this anyway. Not a huge deal, I just liked the custom_error_message syntax but not enough to make custom_error_message Rails 4 compatible myself. – max Feb 21 '14 at 20:02

1 Answers1

7

Try This:

validates :form_person, :presence => {message: "^We need to know who is filling in this form (your name)"}
Emu
  • 5,763
  • 3
  • 31
  • 51
  • 1
    Same result :(. Both the syntax I used and your syntax *used* to work, I could almost swear, in Rails 3. – max Feb 20 '14 at 19:03
  • 1
    Another way is shown to http://stackoverflow.com/questions/808547/fully-custom-validation-error-message-with-rails – Emu Feb 20 '14 at 19:08
  • Okay I see what's going on here. The legacy codebase I'm updating used the custom_error_message gem mentioned in that other answer. Since one of my complaints with the old codebase was that it included too many gems, and since it does not appear to be compatible with Rails 4, I will just write custom validators or something. Thanks for y'all's help! – max Feb 20 '14 at 19:18