Customizing validation msg

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

You can add the following

validates_presence_of :city, :message => " Please enter city "

Regards
Hansraj

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”

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
It’s quite easy.

Paolo

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs