7

When you set a validation message in paperclip, such as

validates_attachment_presence, :image, :message => 'xxxx'

The custom message comes automatically prefixed with the name of the field, even though it has been overwritten with the :message . How do you totally override the message and make it totally custom?

Edit: typo

CafeHey
  • 5,699
  • 19
  • 82
  • 145

4 Answers4

7

Not a real solution but a Easy one is to skip paperclip validation and write custom one.

validate :check_content_type

  def check_content_type
   if !['image/jpeg', 'image/gif','image/png'].include?(self.image_content_type)
    errors.add_to_base("Image '#{self.image_file_name}' is not a valid image type") # or errors.add
   end
  end 

I hope it can help

andrea
  • 3,515
  • 2
  • 22
  • 14
3

You actually want to do this inside your view rather than your model and it's actually quite straight forward. We're just going to loop through the errors, and when the one for your attachment comes up we'll ignore the field name:

<ul>
  <% @myObject.errors.keys.each do |field| %>
    <% @myObject.errors[field].each do |msg| %>
      <% if field == :image_file_name %>
        <li><%= msg %></li>
      <% else %>
        <li><%= field.to_s + " " + msg %></li>
      <% end %>
    <% end %>
  <% end %>
</ul>

Replacing @myObject with the name of your model that should display only the message set to your attachment validation errors. This is just a simple example that displays them inline with the rest, but of course you could do anything you like with the messages. It's important to keep the name of the field that had the error in case you want to program any logic thats specific to its failure without having to rely on the error message staying exactly the same forever.

greggreg
  • 11,945
  • 6
  • 37
  • 52
1

It's standard Rails behavior to show include the attribute name before the validation errors. You have a few options to work around this behavior:

  1. Make your error message OK to have the attribute name prepended :)

  2. Use a different error message formatter. It's pretty easy to write your own helper to iterate through an @object.errors and wrap messages in HTML tags. I prefer to use the error messages in-line near the fields so we always skip the attribute name.

  3. Custom validation which adds the errors to base. This is easy, but wrong, since you're suggesting there's a validation error on a field. Still may solve your problem.

  4. Override humanized_attribute_name for that attribute to hide it. humanized_attribute_name is probably used elsewhere, so this may cause other issues.

.

HumanizedAttributesOverride = {
  :image => ""
}

def self.human_attribute_name(attr)
  HumanizedAttributesOverride[attr.to_sym] || super
end
Community
  • 1
  • 1
jqr
  • 654
  • 6
  • 11
  • The attribute name is getting added to the errors to base set of errors not individual fields but, the fields name is showing up in the base errors area. – CafeHey Jan 17 '11 at 15:46
0

I don't know if it's just a typo inside your question, but it should be:

validates_attachment_presence :image, :message => 'xxxx'

And I would not use :message to add a custom error message, but put it inside a locales file.

RobinBrouwer
  • 973
  • 6
  • 13
  • Weird. I just tested it and I see the message just fine. Thing is, the error is being added to 'image_file_name', so you could just add a simple Rails validation on that column (not sure if it works) and add a :message to that one. Still, it's better to do all the messages inside a locales file. And what version of Paperclip are you using? Because it could be a bug in an older version. – RobinBrouwer Jan 07 '11 at 20:06
  • Just reread your post and I think I misinterpreted it. The message is being shown, but the column name is also being shown in the message. I guess that's an 'error_messages_for' thing. I usually don't use 'error_message_for' (also because it isn't in Rails 3), so I didn't see that in my little test. Don't know how to fix it for you. Sorry about that. :( – RobinBrouwer Jan 07 '11 at 20:16