6

Is it possible to enforce a 'content type' validation in paperclip without enforcing a 'presence' validation (i.e. allow blanks)? I currently have:

class Person < ActiveRecord::Base
  has_attached_file :picture
  validates_attachment_content_type :picture, :content_type => ['image/jpeg', 'image/jpg', 'image/png']
end

However, this fails if no attachment is present. For example:

>> @person = Person.new
>> @person.save
>> @person.errors.first
=> ["picture_content_type", "is not one of image/jpeg, image/jpg, image/png"]

Is it possible to do the validation only if an attachment is included.

Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232

4 Answers4

12

I'm not sure that method is the cause of your failure; Here's my simple class

class Image < ActiveRecord::Base
  has_attached_file :photo, {
            :styles => { :large => "700x400#", :medium=>"490x368#", :thumbnail=>"75x75#" },
            :default_url => "/images/thumbnail/blank-recipe.png"}
  validates_attachment_content_type :photo, :content_type => /image/ 
end

Then, if I:

Image.new.valid?
#this is true

You might be doing other paperclip validations, though. Can you post a simple example?

Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
  • I updated the sample code above slightly. When I perform 'Person.new.valid?' the result is false. I'm running Rails 2.3.8 and Paperclip 2.2.15. – Kevin Sylvestre Jul 06 '10 at 00:47
  • If I understand you correctly `:content_type => /image/` represents a list of image file types. Do you know what that list includes, where in the Paperclip code it is stored and if this exits for other kinds of files such as documents? – Btuman Jun 19 '13 at 17:18
  • @Btuman https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/validators/attachment_content_type_validator.rb#L70 – Jesse Wolgamott Jun 19 '13 at 19:11
  • Watch out of last `}` in `:default_url => "/images/thumbnail/blank-recipe.png"}` This will cause error. – Muhammad Nasir Shamshad Mar 24 '18 at 20:31
10

Working example

In the following model only image/png, image/gif and image/jpeg are valid content types for the image attachment.

class Photo
  has_attached_file :image
  validates_attachment_content_type :image, 
                                    :content_type => /^image\/(png|gif|jpeg)/
end

Specs

describe Photo do
  it { should validate_attachment_content_type(:image).  
              allowing('image/png', 'image/gif', 'image/jpeg').      
              rejecting('text/plain', 'text/xml', 'image/abc', 'some_image/png') }
end

More info

You could also take a look at the AttachmentContentTypeValidator class with is responsible for doing the validation.

Or take a look at its tests which contain more examples.

mcls
  • 9,071
  • 2
  • 29
  • 28
1

validates_content_type accepts :if => Proc.new{|r| !r.content_type.blank?} in it's options hash, perhaps that would solve your problem.

http://rdoc.info/github/thoughtbot/paperclip#

Tim Snowhite
  • 3,736
  • 2
  • 23
  • 27
0

This worked for me;

validates_attachment :image1, :presence => true,
                         :content_type => { :content_type => "image/jpg" },
                         :size => { :in => 0..10.kilobytes }
Bernard Banta
  • 755
  • 8
  • 14