Need a url rich-type
Reported by Tom Locke | September 9th, 2008 @ 11:09 AM | in Beyond Hobo 1.0
Would provide
-
URL regex validation
-
Automatically add http:// if omitted
-
Render as link
Comments and changes to this ticket
-
Tom Locke September 9th, 2008 @ 11:09 AM
- State changed from new to open
-
Tom Locke September 15th, 2008 @ 05:21 PM
- Milestone changed from Hobo 1.0 - Final to Beyond Hobo 1.0
-
kevinpfromnm July 29th, 2009 @ 05:09 AM
Is this the sort of thing you're looking for?
I tested it and it works with http, https, ftp and no protocol specified which defaults to http://. I also included an alternate that works with ip addresses as well commented out.
-
kevinpfromnm July 30th, 2009 @ 12:23 AM
odd, I was able to download it. I'll reattach it here and inline. I tweaked it a bit with one thing I noticed as a problem where it wouldn't accept blank urls.
module HoboFields
class Url < StringCOLUMN_TYPE = :string HoboFields.register_type(:url, self) def validate return if self.blank? begin uri = URI.parse(self) unless uri.class.in? [URI::HTTP, URI::HTTPS, URI::FTP, URI::Generic] return 'Only HTTP protocol addresses can be used' end rescue URI::InvalidURIError return 'The format of the url is not valid.' end "improper format" unless self =~ /^((ftp|http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?$/ix # alternate that handles IP addresses "improper format" unless self =~ /^((ftp|http|https):\/\/)?([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}|([1-9][0-9]{0,2}\.){3}[1-9][0-9]{0,2})(:[0-9]{1,5})?(\/.*)?$/ix end def to_html "<a href='#{'http://' unless self.slice(/https?:\/\//)}#{self}'>#{self}</a>" unless self.blank? end
end end
-
kevinpfromnm July 30th, 2009 @ 12:25 AM
module HoboFields
class Url < String COLUMN_TYPE = :string HoboFields.register_type(:url, self) def validate return if self.blank? begin uri = URI.parse(self) unless uri.class.in? [URI::HTTP, URI::HTTPS, URI::FTP, URI::Generic] return 'Only HTTP protocol addresses can be used' end rescue URI::InvalidURIError return 'The format of the url is not valid.' end "improper format" unless self =~ /^((ftp|http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?$/ix # alternate that handles IP addresses "improper format" unless self =~ /^((ftp|http|https):\/\/)?([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}|([1-9][0-9]{0,2}\.){3}[1-9][0-9]{0,2})(:[0-9]{1,5})?(\/.*)?$/ix end def to_html "<a href='#{'http://' unless self.slice(/https?:\/\//)}#{self}'>#{self}</a>" unless self.blank? end end end
once more for markdown
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป