Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Jesse Newland http://jnewland.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Rails URL Validation

No regexes, allows URLs with ports or IPs. Inspiration from here

   1  
   2    validates_each :href, :on => :create do |record, attr, value|
   3      begin
   4        uri = URI.parse(value)
   5        if uri.class != URI::HTTP
   6          record.errors.add(attr, 'Only HTTP protocol addresses can be used')
   7        end
   8      rescue URI::InvalidURIError
   9        record.errors.add(attr, 'The format of the url is not valid.')
  10      end
  11    end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS