# Customizing validation msg

**URL:** <https://www.ruby-forum.com/t/customizing-validation-msg/182811>\
**Category:** Rails\
**Created:** [February 3, 2010, 6:34am UTC](https://www.ruby-forum.com/t/customizing-validation-msg/182811 "2010-02-03T06:34:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_I\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@M\_I\_R](https://www.ruby-forum.com/u/M_I_R)\
**Post date:** [February 3, 2010, 6:34am UTC](https://www.ruby-forum.com/t/customizing-validation-msg/182811/1 "2010-02-03T06:34:40Z")

</div>

Hi,  
i am using the default validation of RoR how i will customize them i  
dont want to display the default message but i want to print the  
message i want like for city\_id the default message is “City can’t be  
blank” i want this message as “Please enter city”  
Regards

---

<div class="post-metadata">

**Author:** ![M\_I\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@M\_I\_R](https://www.ruby-forum.com/u/M_I_R)\
**Post date:** [February 3, 2010, 9:18am UTC](https://www.ruby-forum.com/t/customizing-validation-msg/182811/2 "2010-02-03T09:18:47Z")

</div>

You can add the following

validates\_presence\_of :city, :message =\> " Please enter city "

Regards  
Hansraj

---

<div class="post-metadata">

**Author:** ![M\_I\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@M\_I\_R](https://www.ruby-forum.com/u/M_I_R)\
**Post date:** [February 3, 2010, 1:39pm UTC](https://www.ruby-forum.com/t/customizing-validation-msg/182811/3 "2010-02-03T13:39:14Z")

</div>

i try this one but it give an issue it first display city and then the  
message for your suggestion it will dispaly “City please enter the  
city”

---

<div class="post-metadata">

**Author:** ![M\_I\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@M\_I\_R](https://www.ruby-forum.com/u/M_I_R)\
**Post date:** [February 3, 2010, 2:01pm UTC](https://www.ruby-forum.com/t/customizing-validation-msg/182811/4 "2010-02-03T14:01:38Z")

</div>

M I R wrote:

> i try this one but it give an issue it first display city and then the  
> message for your suggestion it will dispaly “City please enter the  
> city”

The error\_messages\_for helper displays the name of the model attribute  
(the field of the database) followed by the message set in the  
validation line. You can roll out your alternative to error\_messages\_for  
by using the methods listed at  
[http://api.rubyonrails.org/classes/ActiveRecord/Errors.html](http://api.rubyonrails.org/classes/ActiveRecord/Errors.html)  
It’s quite easy.

Paolo

---

<div class="post-metadata">

**Author:** ![M\_I\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@M\_I\_R](https://www.ruby-forum.com/u/M_I_R)\
**Post date:** [February 3, 2010, 2:04pm UTC](https://www.ruby-forum.com/t/customizing-validation-msg/182811/5 "2010-02-03T14:04:09Z")

</div>

Instead of using validates\_presence\_of you could do the validation  
manually and customize the error message.

private  
def validate  
if(self.city == nil || self.city == ‘’)  
errors.add\_to\_base(“Please enter city”)  
end  
end

However, by using “add\_to\_base” you will not be able to highlight which  
field contains the error.
