#35 ✓resolved

override error message for validations

Reported by alterecco | October 29th, 2007 @ 12:03 PM

It would be nice to be able to set your own error messages for failed validations.

Perhaps like in AR:

validates_format_of :email, :as => :email_address, :context => :save, :message => "#{value} ain't an email!!"

Comments and changes to this ticket

  • Sam Smoot

    Sam Smoot October 29th, 2007 @ 11:43 PM

    • Assigned user set to “Sam Smoot”
    • State changed from “new” to “open”

    Yes it would... translation hooks would be nice as well. Like #t or something. Ideas? Is there a way to specify variable index when formatting strings in Ruby? Like so:


    String.Format("{0} is {1}!", "Sam", 30);

    So if you want to swap around the position of arguments, you can. Which helps with some translations. Anything similar for Ruby?

  • Brian Rose

    Brian Rose October 30th, 2007 @ 11:54 AM

    Getting my foot in door. ;)

    I'm not aware of a built-in method to replace on identifiers, but it seemed like something simple enough.

    Some additional matching logic could be added to auto-capitalize a value if it's at the beginning of a sentence, etc.

  • Sam Smoot

    Sam Smoot November 2nd, 2007 @ 02:33 PM

    This is apparently built-in to Ruby:

    "%s %s %s" % %w(one two three) #=> "one two three"
    "%3$s %2$s %1$s" % %w(one two three) #=> "three two one"

    Now I know what you're thinking, "Sam, that's really ugly though!". I agree. But then again, it's documented, and common knowledge-ish, so it seems the safe route.

  • Brian Rose

    Brian Rose November 7th, 2007 @ 08:58 AM

    Yup, ugly as sin, but it works and doesn't take much research to figure what's going on. :)

  • Sam Smoot

    Sam Smoot November 2nd, 2007 @ 04:29 PM

    • State changed from “open” to “resolved”


    Just overwrite String::translate(value) to plugin your preferred form of storage (yaml, constants, database lookup, whatever).

    it 'should translate error messages' do
      String::translations["%s must not be blank"] = "%s should not be blank!"
      beth = Cow.new
      beth.age = 30
      beth.should_not be_valid
      beth.errors.full_messages.should include('Name should not be blank!')
      String::translations.delete("%s must not be blank")
  • Nick Plante

    Nick Plante November 7th, 2007 @ 11:42 AM

    • no changes were found...

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket