# Adding custom message on model validation in rails 3

**URL:** <https://www.ruby-forum.com/t/adding-custom-message-on-model-validation-in-rails-3/195249>\
**Category:** Rails\
**Created:** [September 20, 2010, 2:49pm UTC](https://www.ruby-forum.com/t/adding-custom-message-on-model-validation-in-rails-3/195249 "2010-09-20T14:49:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 20, 2010, 2:49pm UTC](https://www.ruby-forum.com/t/adding-custom-message-on-model-validation-in-rails-3/195249/1 "2010-09-20T14:49:57Z")

</div>

Hi,  
I am developing an application in rails 3.0

I have post Model  
class Post \< ActiveRecord::Base  
validates :name , :presence =\> true  
end

I have added the presence of validation on post name.  
It shows me error that “Name can’t be blank”

I have to give the custom message for the user so I have written like as  
follows:-

class Post \< ActiveRecord::Base  
validates :name , :presence =\> true , :message=\>“Post name can’t be  
blank.”  
end

But it is giving me error:-  
“Unknown validator: ‘message’”

Can anyone tell me How to give the custom message in rails 3 model  
validations?

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 20, 2010, 3:38pm UTC](https://www.ruby-forum.com/t/adding-custom-message-on-model-validation-in-rails-3/195249/2 "2010-09-20T15:38:48Z")

</div>

Hey,  
I don’t want to use the javascript, as rails directly provides us  
validations then why should we write code.

Is there anyone know how to add custom message on model validation in  
rails 3.0.0

Thanks,  
Mike.

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 20, 2010, 3:11pm UTC](https://www.ruby-forum.com/t/adding-custom-message-on-model-validation-in-rails-3/195249/3 "2010-09-20T15:11:27Z")

</div>

I think differently: Why can’t you use JavaScript to check for a blank  
field?

Regards,

* * *

Edmond  
Software Developer | Baobab Health Trust ([http://www.baobabhealth.org/](http://www.baobabhealth.org/))  
|  
Malawi

Cell: +265 999 465 137 | +265 881 234 717

_“Many people doubt open source software and probably donâ€™t realize that  
there is an alternativeâ€¦ which is just as good…” – Kevin Scannell_

2010/9/20 Mike D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 20, 2010, 7:47pm UTC](https://www.ruby-forum.com/t/adding-custom-message-on-model-validation-in-rails-3/195249/4 "2010-09-20T19:47:25Z")

</div>

Please quote when replying.

Edmond K. wrote:

> I think differently: Why can’t you use JavaScript to check for a blank  
> field?

No way! JavaScript validations should never be relied on – it’s too  
easy for the client to turn them off and bypass them, or to submit a  
crafted GET or POST request that bypasses the HTML form entirely.  
Validation _must_ be done on the server side.

> Regards,
> 
> * * *
> 
> Edmond

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 20, 2010, 6:42pm UTC](https://www.ruby-forum.com/t/adding-custom-message-on-model-validation-in-rails-3/195249/5 "2010-09-20T18:42:48Z")

</div>

Hi,

try the following:

class Post \< ActiveRecord::Base  
validates :name , :presence =\> { :message=\>“Post name can’t be  
blank.”  
}  
end

Maksim.

Am 20.09.2010 14:49, schrieb Mike D.:

> Can anyone tell me How to give the custom message in rails 3 model  
> validations?
> 
> Thanks,  
> Mike

–  
Maksim G.  
webit! Gesellschaft für neue Medien mbH  
Schnorrstraße 76 | 01069 Dresden  
Telefon +49 351 46766-0 | Telefax +49 351 46766-66  
removed\_email\_address@domain.invalid | [www.webit.de](http://www.webit.de)

Amtsgericht Dresden | HRB 15422  
USt-ID DE 193477690  
Geschäftsführer Sven Haubold

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 23, 2010, 12:09pm UTC](https://www.ruby-forum.com/t/adding-custom-message-on-model-validation-in-rails-3/195249/6 "2010-09-23T12:09:34Z")

</div>

Hi,  
Your solution is working but the message is coming like this:-

“Name Post name can’t be blank.”

My model post.rb is:-

class Post \< ActiveRecord::Base  
validates :name , :presence =\> { :message=\>“Post name can’t be  
blank.”}  
validates :title, :presence =\> true, :length =\> { :minimum =\> 5 }  
has\_many :comments , :dependent =\> :destroy  
end

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 23, 2010, 12:23pm UTC](https://www.ruby-forum.com/t/adding-custom-message-on-model-validation-in-rails-3/195249/7 "2010-09-23T12:23:46Z")

</div>

On 23 September 2010 11:09, Mike D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> Your solution is working but the message is coming like this:-

What solution, from whom? You did not quote the previous message so  
no-one knows what you are talking about.

Colin

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 23, 2010, 2:15pm UTC](https://www.ruby-forum.com/t/adding-custom-message-on-model-validation-in-rails-3/195249/8 "2010-09-23T14:15:26Z")

</div>

Yes.  
I am talking about the Maksim solution.

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 23, 2010, 2:39pm UTC](https://www.ruby-forum.com/t/adding-custom-message-on-model-validation-in-rails-3/195249/9 "2010-09-23T14:39:01Z")

</div>

Hi,

it’s the default error\_message\_on behavior. You can try to override it.  
Another solution could be to write your message without the attribute:  
just “can’t be blank.”, so you get “Name can’t be blank”.

Hope it helps you. Regards,  
Maksim.

Am 23.09.2010 12:09, schrieb Mike D.:
