# Modifying error\_messages\_for - I'm completely stuck

**URL:** <https://www.ruby-forum.com/t/modifying-error-messages-for-im-completely-stuck/108359>\
**Category:** Rails\
**Created:** [July 2, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/modifying-error-messages-for-im-completely-stuck/108359 "2007-07-02T19:07:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [July 2, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/modifying-error-messages-for-im-completely-stuck/108359/1 "2007-07-02T19:07:52Z")

</div>

To cope with languages other than English, it seems to be necessary to  
modify error\_messages\_for by defining a new application helper. Tricky  
stuff for a novice like me!

I need to get all of the text out so that I can make it language  
dependent. I have used instance variables at the moment, but will get  
them out of the helper once I have a solution. The problem I have is  
with the error lines such as the following, which contain a field name  
as a prefix:

Terms : You must accept the Terms and Conditions

The following regular expression should work on the above line I think  
(i.e. to get rid of “Terms :” so that all remains is the message passed  
from the validation :message in the model

[A-Za-z0-9|]\*$

So I am trying to do the following to a line near the bottom of the  
script

```
        error_messages = objects.map {|object|

```

object.errors.full\_messages.map {|msg| content\_tag(:li, /[A-Za-z0-9|  
]\*$/.match(msg)) } }

I am obviously making a mistake here, because it gives nothing except a  
bullet point, but I am not sure what. Does anyone have an idea where I  
am going wrong?

Any help would be really appreciated.

Darren

def my\_error\_messages\_for(\*params)  
@e1=‘There were problems with the following fields:’  
@e2=‘errors’  
@e3=‘prohibited this from being saved’  
options = params.last.is\_a?(Hash) ? params.pop.symbolize\_keys : {}  
objects = params.collect {|object\_name|  
instance\_variable\_get("@#{object\_name}") }.compact  
count = objects.inject(0) {|sum, object| sum +  
object.errors.count }  
unless count.zero?  
html = {}  
[:id, :class].each do |key|  
if options.include?(key)  
value = options[key]  
html[key] = value unless value.blank?  
else  
html[key] = ‘errorExplanation’  
end  
end

```
         header_message = "#{pluralize(count, @e2)} #{@e3}"
        error_messages = objects.map {|object|

```

object.errors.full\_messages.map {|msg| content\_tag(:li, msg) } }  
content\_tag(:div,  
content\_tag(options[:header\_tag] || :h2, header\_message)  
\<\<  
content\_tag(:p, @e1) \<\<  
content\_tag(:p, error\_messages),  
html  
)  
else  
‘’  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [July 6, 2007, 7:32am UTC](https://www.ruby-forum.com/t/modifying-error-messages-for-im-completely-stuck/108359/2 "2007-07-06T07:32:08Z")

</div>

It’s 3 days later and I have not had a reply yet. I have still not made  
any progress. I hope that someone can help me a little with this.

Thanks

Darren

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [July 6, 2007, 7:58am UTC](https://www.ruby-forum.com/t/modifying-error-messages-for-im-completely-stuck/108359/3 "2007-07-06T07:58:15Z")

</div>

you can remove the prefix from the field name by using the  
custom-err-msg plugin ([http://rubyforge.org/projects/custom-err-msg/](http://rubyforge.org/projects/custom-err-msg/))

Mike

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [July 6, 2007, 8:15am UTC](https://www.ruby-forum.com/t/modifying-error-messages-for-im-completely-stuck/108359/4 "2007-07-06T08:15:45Z")

</div>

Mike

Looks like excellent advice! I will give it a go!

Many thanks

Darren

Mike G. wrote:

> you can remove the prefix from the field name by using the  
> custom-err-msg plugin ([http://rubyforge.org/projects/custom-err-msg/](http://rubyforge.org/projects/custom-err-msg/))
> 
> Mike

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [July 7, 2007, 10:58am UTC](https://www.ruby-forum.com/t/modifying-error-messages-for-im-completely-stuck/108359/5 "2007-07-07T10:58:16Z")

</div>

This part works, but now I have problems with variables in models  
associated with this. How can I get @message1 to @message13 out of the  
model so that I can define them using a database query? I have a session  
variable called language\_id which defines the language, but I can’t  
access it from the model of course. I would like to pass the variables  
to the model, but how can I do that when the code is not within a  
def…end?

Thanks!

require ‘digest/sha1’  
class User \< ActiveRecord::Base  
require\_dependency “search”

searches\_on :firstname, :lastname, :formally\_known\_as  
has\_many :friends, :dependent =\> :destroy  
has\_many :logins, :dependent =\> :destroy  
has\_many :usercategories, :dependent =\> :destroy  
has\_many :userimages, :dependent =\> :destroy  
has\_many :usernotifications, :dependent =\> :destroy  
has\_many :userplaces, :dependent =\> :destroy

has\_many :sent\_messages, :class\_name =\> “Message”, :foreign\_key =\>  
“sender\_id”, :order =\> ‘created\_at DESC’  
has\_many :received\_messages, :class\_name =\> “Message”, :foreign\_key =\>  
“recipient\_id”, :conditions =\> [‘deleted\_at IS NULL’], :order =\>  
‘created\_at DESC’  
has\_many :unread\_messages, :class\_name =\> “Message”, :foreign\_key =\>  
“recipient\_id”, :conditions =\> [‘read\_at IS NULL AND deleted\_at IS  
NULL’], :order =\> ‘created\_at DESC’

@message1=“^You must enter your first name.”  
@message2=“^You must enter your last name.”  
@message3=“^You must enter your email address.”  
@message4=“^Your username must be at least 6 characters long.”  
@message5=“^Someone with your username is already registered with us,  
please choose another.”  
@message6=“^You must enter a username.”  
@message7=“^Your e-mail address is already registered with us. Please  
select the ‘forgot password’ link on login box to be resent your  
username and password.”  
@message8=“^You must enter a password.”  
@message9=“^Your e-mail address is not a valid format.”  
@message10=“^Your password must be at least 6 characters long.”  
@message11=“^You must enter confirmation of your password.”  
@message12=“^Your password and confirmation were not the same.”  
@message13=“^You must accept the Terms and Conditions to register.”

validates\_presence\_of :firstname, :message =\> @message1  
validates\_presence\_of :lastname, :message =\> @message2  
validates\_presence\_of :email, :message =\> @message3  
validates\_length\_of :username, :minimum =\> 6, :message =\> @message4  
validates\_uniqueness\_of :username, :message =\> @message5  
validates\_presence\_of :username, :message =\> @message6  
validates\_uniqueness\_of :email, :message =\> @message7  
validates\_presence\_of :password, :on =\> :create, :message =\>  
@message8  
validates\_format\_of :email, :message =\> @message9, :with =\>  
/^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i  
validates\_length\_of :password, :minimum =\> 6, :message =\> @message10,  
:on =\> :create  
validates\_presence\_of :hashed\_password, :on =\> :create, :message =\>  
@message11  
validates\_confirmation\_of :password, :on =\> :create, :message =\>  
@message12  
validates\_acceptance\_of :term, :on =\> :create, :message =\> @message13

attr\_accessor :password\_confirmation

Darren E. wrote:

> Mike
> 
> Looks like excellent advice! I will give it a go!
> 
> Many thanks
> 
> Darren
> 
> Mike G. wrote:
> 
> > you can remove the prefix from the field name by using the  
> > custom-err-msg plugin ([http://rubyforge.org/projects/custom-err-msg/](http://rubyforge.org/projects/custom-err-msg/))
> > 
> > Mike

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [July 8, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/modifying-error-messages-for-im-completely-stuck/108359/6 "2007-07-08T14:35:59Z")

</div>

Has anyone come across this before? How can I pass variables to  
validates… in a model, the problem being that it is at the first  
level and I cannot use session variables.

Darren

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [July 8, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/modifying-error-messages-for-im-completely-stuck/108359/7 "2007-07-08T15:10:00Z")

</div>

Thanks, I’ll take a look there and can hopefully find something there. I  
wasn’t aware of this forum.

Just so that I understand properly, the answer to my previous question  
is “no, because it is against the ROR philosophy”. Is that right?

Darren

Perry S. wrote:

> Darren E. wrote:
> 
> > Has anyone come across this before? How can I pass variables to  
> > validates… in a model, the problem being that it is at the first  
> > level and I cannot use session variables.
> 
> Darren,
> 
> Look at this forum:
> 
> [http://www.ruby-forum.com/forum/20](http://www.ruby-forum.com/forum/20)
> 
> It is called “Ruby I18N” which should be exactly the place for you.
> 
> Good luck

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [July 8, 2007, 2:53pm UTC](https://www.ruby-forum.com/t/modifying-error-messages-for-im-completely-stuck/108359/8 "2007-07-08T14:53:32Z")

</div>

Darren E. wrote:

> Has anyone come across this before? How can I pass variables to  
> validates… in a model, the problem being that it is at the first  
> level and I cannot use session variables.

Darren,

Look at this forum:

[http://www.ruby-forum.com/forum/20](http://www.ruby-forum.com/forum/20)

It is called “Ruby I18N” which should be exactly the place for you.

Good luck

---

<div class="post-metadata">

**Author:** ![darrenevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@darrenevans](https://www.ruby-forum.com/u/darrenevans)\
**Post date:** [July 8, 2007, 3:33pm UTC](https://www.ruby-forum.com/t/modifying-error-messages-for-im-completely-stuck/108359/9 "2007-07-08T15:33:27Z")

</div>

Darren E. wrote:

> Just so that I understand properly, the answer to my previous question  
> is “no, because it is against the ROR philosophy”. Is that right?

I honestly am not the best person to ask. I notice a few things. You  
are passing instance variables. I don’t know how the validates\_…  
things work but since they are out side of any execution path, I would  
assume they set up something at load time. This is way before any  
instances have been created. I’m surprised it works really.

You could give it @@variable – class variables. These will stay put.  
Then, somehow, magically make the @@variable point to an @variable that  
will change based upon the users language. There are all sorts of hooks  
– maybe put that into before\_validate.

But, I would try to go for some table driven solution. (This may be  
what you are trying to do after you get something – anything –  
working.) An array of an array of string. Where the first index is the  
language (could be a hash), the second index is a message number (or a  
hash).

And, my other thought, while validates\_… are easy, you can put all  
this into a validate method. At that point, you have the instance  
context.

This is documented in callbacks.rb but you have all these hookds:

# \* (-) save

# \* (-) valid?

# \* (1) before\_validation

# \* (2) before\_validation\_on\_create

# \* (-) validate

# \* (-) validate\_on\_create

# \* (3) after\_validation

# \* (4) after\_validation\_on\_create

# \* (5) before\_save

# \* (6) before\_create

# \* (-) create

# \* (7) after\_create

# \* (8) after\_save

(and many others if this is an updates or a delete).

Good luck
