title: Rails validation of image simple_form
body: <p>I have that problem that my simple_form require both image file upload field and image url input. </p>
  
  <p>How do I validate so that it is either the image fileupload field or the image url that should be requried not the both.</p>
  
  <p>My View in another controller:</p>
  
  <pre><code>            &lt;%= f.simple_fields_for :photo_attributes do |d| %&gt;
  &lt;%= d.label :image, :label =&gt; 'Upload logo'  %&gt;
  &lt;%= d.file_field :image, :label =&gt; 'Image'  %&gt;
  &lt;%= d.input :image_url, :label =&gt; 'Billed URL' %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>My photo model:</p>
  
  <pre><code>require 'open-uri'
  
  class Photo &lt; ActiveRecord::Base
    belongs_to :virksomhed
    attr_accessor :image_url
  
    has_attached_file :image,
                    :url  =&gt; "/public/images/billeder/photo/:id/:basename.:extension",
                    :path =&gt; ":rails_root/public/images/:id/:basename.:extension"
  
    before_validation :download_remote_image, :if =&gt; :image_url_provided?
  
    validates_presence_of :image_remote_url, :if =&gt; :image_url_provided?, :message =&gt; 'is invalid or inaccessible'
  
  private
  
    def image_url_provided?
      !self.image_url.blank?
    end
  
    def download_remote_image
      self.image = do_download_remote_image
      self.image_remote_url = image_url
    end
  
    def do_download_remote_image
      io = open(URI.parse(image_url))
      def io.original_filename; base_uri.path.split('/').last; end
      io.original_filename.blank? ? nil : io
    rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
    end
  
  end
  </code></pre>