skip carrierwave Integirty and Processing validation

依然范特西╮ 提交于 2019-12-12 15:24:00

问题


I have white listed some of the extensions in the carrierwave uploader class

def extension_white_list
    %w(doc docx)
end

In some cases I would like to skip the Integrity validation while saving a record. But as per their documentation validates_integrity_of validation exist by default.

https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Validate-uploads-with-Active-Record

can anyone please tell me how to skip such validation ?


回答1:


in uploaders/file_uploader.rb

def extension_white_list
  model.do_i_need_validation?
    %w(doc docx)
  else
   file.extension
  end
end

and define this instance method in the model

def do_i_need_validation?
  condition? ? true : false
end

Just replace the content of the method suitable to your app




回答2:


I couldn't find anything about this in any of carrierwave's documentation, but reading its source code, one can pass specific uploader options in the mount_uploader call:

mount_uploader :field, MyUploader, options

Validations configuration do exist in uploader options, so you can, for example, disable all validations using:

mount_uploader :field, MyUploader, validate_download: false, validate_integrity: false, validate_processing: false

Note that when doing this the errors are silently ignored, so saves will succeed. This could be unexpected behavior. You can check if the operation actually had any errors using the model helpers <field>_processing_error, <field>_integrity_error and <field>_download_error:

class Article < ActiveRecord::Base
  mount_uploader :image, ImageUploader, validate_integrity: false
end

article = Article.find(1)
article.update_attributes!(title: "New article title", image: open("/path/to/invalid_image.jpg")) # => this will actually succeed
article.image_integrity_error # => returns the error message from carrierwave


来源:https://stackoverflow.com/questions/17233816/skip-carrierwave-integirty-and-processing-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!