Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Make ActiveRecord errors support full sentence messages

#135

Current Rails is extremely strict about what an attribute-specific error message looks like. A human-readable attribute name is always the first word(s) of the sentence, and a developer is only able to customize the sentence fragment after this word.

This makes it difficult to localize error messages to non-English languages, and many error messages end up sounding more kludgey than they could because of the "first word being an attribute name" restriction.

We're not addressing the entire issue with this patch - the Errors object isn't flexible enough to customize the format, as the only information we can pass per error is an attribute/string message combo.

What we did do is change the format of :message to accept a full sentence with string substitution for the symbol ":attribute". So, instead of

object.errors.add(:first_name, "can't be blank")

you could do

object.errors.add(:first_name, ":attribute can't be blank")

or

object.errors.add(:first_name, "Why are you making your :attribute blank?")

or

object.errors.add(:first_name, "People who don't change their name prefix are bad people.")

The :message parameter in validations work similarly. All default messages yield the same sentences as before.

Reported by Nathan Wilmes · May 7th, 2008 @ 07:50 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Nathan Wilmes
  2. Pratik
    Pratik
    • State changed from new to hold

    We're revamping AR::Errors class with ActiveModel. So I'm keeping this on hold.

    Thanks.

    May 11th, 2008 @ 08:39 PM

  3. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from hold to resolved
    • Tag set to activerecord, errors, patch, tested, validation

    No longer needed due to changes since.

    August 3rd, 2009 @ 03:20 PM