Created
December 4, 2012 02:30
-
-
Save chourobin/4199997 to your computer and use it in GitHub Desktop.
Subdomain Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://matthewhutchinson.net/2010/10/27/rails-3-subdomain-validation-activemodeleachvalidator | |
# subdomain_validator.rb (place in your lib/ or extra/ load path) | |
class SubdomainValidator < ActiveModel::EachValidator | |
def validate_each(object, attribute, value) | |
return unless value.present? | |
reserved_names = %w(www ftp mail pop smtp admin ssl sftp) | |
reserved_names = options[:reserved] if options[:reserved] | |
if reserved_names.include?(value) | |
object.errors[attribute] << 'cannot be a reserved name' | |
end | |
object.errors[attribute] << 'must have between 3 and 63 letters' unless (3..63) === value.length | |
object.errors[attribute] << 'cannot start or end with a hyphen' unless value =~ /^[^-].*[^-]$/i | |
object.errors[attribute] << 'must be alphanumeric; A-Z, 0-9 or hyphen' unless value =~ /^[a-z0-9\-]*$/i | |
end | |
end | |
# And in your model | |
validates :subdomain, :presence => true, | |
:uniqueness => true, | |
:subdomain => true | |
# Or with your own reserved names | |
validates :subdomain, :presence => true, | |
:uniqueness => true, | |
:subdomain => { :reserved => %w(foo bar) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment