I work with the code you provided and some of the code provided in the CarrierWave Wiki to check for remote URLs .
You can create a new validator in lib/remote_image_validator.rb .
require 'fastimage' class RemoteImageValidator < ActiveModel::EachValidator def validate_each(object, attribute, value) raise(ArgumentError, "A regular expression must be supplied as the :format option of the options hash") unless options[:format].nil? || options[:format].is_a?(Regexp) configuration = { :message => "is invalid or not responding", :format => URI::regexp(%w(http https)) } configuration.update(options) if value =~ configuration[:format] begin if FastImage.type(CGI::unescape(avatar_remote_url)) true else object.errors.add(attribute, configuration[:message]) and false end rescue object.errors.add(attribute, configuration[:message]) and false end else object.errors.add(attribute, configuration[:message]) and false end end end
Then in your model
class User < ActiveRecord::Base validates :avatar_remote_url, :remote_image => { :format => /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[az]{2,5}(([0-9]{1,5})?\/.*)?$)/ix, :unless => remote_avatar_url.blank? } end
source share