Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Rails validation helpers :message but want it without listing the column name in message

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)

like image 961
daustin777 Avatar asked Mar 27 '09 15:03

daustin777


4 Answers

I know this question is old. But just for reference in case someone else stumbles over it as I just did.

At least in Rails 3.0.x (not sure about earlier versions) you can use the leading ^ as indicated by RadBrad without the need for any gems/plugins.

like image 57
Hartwig Avatar answered Nov 14 '22 22:11

Hartwig


Rails 4 answer

Took a look at the code here:

https://github.com/rails/rails/blob/master/activemodel/lib/active_model/errors.rb#L374

You can therefore set in your en.yml file:

en:
  errors:
    format: '%{message}'

This means you need to also set the full error message everywhere else but I guess this is preferable. Note, and I found this confusing, the errors format is not in the ActiveRecord namespace where I generally put the rest of my error messages.

like image 8
TechEddy Avatar answered Nov 15 '22 00:11

TechEddy


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

https://github.com/jeremydurham/custom-err-msg

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.

like image 6
RadBrad Avatar answered Nov 14 '22 22:11

RadBrad


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.

like image 3
Mark Swardstrom Avatar answered Nov 14 '22 22:11

Mark Swardstrom