views:

2921

answers:

5

Using Rails validation helpers-

validates_xxx_of :the_column, :message => "my message"

will generate a validation message :

*the_column my message*

Is there a way to turn-off the inclusion of the column name? (substitute xxx with any validation helper method)

+3  A: 

Take a look at this blog for starters, and this one as a chaser.

MarkusQ
I used the Human Attribute Override plugin:script/plugin install http://svn.redlinesoftware.com/plugins/human_attribute_override
daustin777
+1  A: 

The way that I did this was override ALL the messages, and not use the Rails form helpers for displaying error messages.

This seems like a lot of work, but it actually has some nice benefits. You get full control of the message, and then you can implement a custom form builder that can put the error messages inline, which is nicer for the user.

You use it like this:

validates_uniqueness_of :foobar, :message => "The foobar isn't unique."

Then don't use full_messages when printing the error message.

Luke Francl
A: 

There is a customer error message plugin that should do what you want

http://rubyforge.org/projects/custom-error-message/

It allows you to override the normal message construction, and define the complete message yourself like this:

:message=> "^ Your email address seems rather messed up, please try again"

Notice the ^ character, that tells rails NOT to prepend anything, just use the message exactly as defined, (except it removes the ^)

If you don't put a leading ^, then you get the normal rails generated error message.

RadBrad
+2  A: 

This is the best explanation I could find.

http://adamhooper.com/eng/articles/5

Essentially, in an initializer, change the full_messages method in ActiveRecord.Errors to return full sentences (not column_name, message concatenations) if you give a :message attribute in the validation.

Update - If you try Adam's code, you have to use the en.yml property file, if not it won't work as expected. You can either do this or get around it by modifying the full_messages method further. This works for me. I added the following to an initializer (/imitializers/active_record_errors.rb)

if RAILS_GEM_VERSION =~ /^2\.3/
  ActiveRecord::Errors.class_eval do
    # Remove complicated logic
    def full_messages
      returning full_messages = [] do
        @errors.each_key do |attr|
          @errors[attr].each do |message|
            next unless message
            if attr == "base"
              full_messages << message
            elsif message =~ /^\^/
              full_messages << $'            #' Grabs the text after the '^'
            else 
              attr_name = @base.class.human_attribute_name(attr)
              full_messages << attr_name + I18n.t('activerecord.errors.format.separator', :default => ' ') + message 
            end  
          end 
        end 
      end 
    end 
  end 
end

Adam also makes good arguments for modifying Rails to support this for internationalization efforts.

Swards
+1  A: 

the link to rubyforge doesn't work, here is the custom error message plugin on github:

http://github.com/gumayunov/custom-err-msg

deb