Validate URL
(from https://gist.github.com/bluemont/2986523 and http://stackoverflow.com/questions/7167895/whats-a-good-way-to-validate-links-urls-in-rails-3)Put this into app/validators
class UrlValidator < ActiveModel::EachValidatordef validate_each(record, attribute, value)valid = beginURI.parse(value).kind_of?(URI::HTTP)rescue URI::InvalidURIErrorfalseendunless validrecord.errors[attribute] << (options[:message] || "is an invalid URL")endendend
Usage
validates :url, :presence => true, :url => true
Немає коментарів:
Дописати коментар