1
4

1 回答 1

0

domain_validator.js.coffee

invalid_domains = [
"@myway.com",
"@mail.com" ] # two, for example, out of about 1,000

domains_joined = invalid_domains.join('|')
domains_pattern = new RegExp("^.+(#{domains_joined})$", "i")

clientSideValidations.validators.local["email_domain"] = (element, options) ->
  if (domains_pattern.test(element.val())) 
    options.message()

lib/email_domain_validator.rb

   class EmailDomainValidator < ActiveModel::EachValidator
      INVALID_DOMAINS = %w( 
         @myway.com
         @mail.com ) # two, for example

      def validate_each(object, attribute, value)
        if value =~  /^.+(#{INVALID_DOMAINS.join('|')})$/i
          object.errors.add(attribute, :email_domain, options)
        end
      end
    end

我也没有正确的错误信息。现在我做(email_domain):

配置/语言环境/en.yml

en:
  errors:
    messages:
      email_format: "is not formatted properly"
      email_domain: "this email provider is not allowed" # tada!
于 2011-08-20T10:57:55.733 回答