Class: LoginFormatValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/models/protected/user.rb

Overview

Schema Information

Table name: users

id                     :integer(4)      not null, primary key
first_name             :string(255)
last_name              :string(255)
company                :string(255)
password_salt          :string(255)     default(""), not null
remember_token         :string(255)
first_login            :boolean(1)      default(TRUE)
is_admin               :boolean(1)
created_at             :datetime
updated_at             :datetime
email                  :string(255)     default(""), not null
encrypted_password     :string(128)     default(""), not null
reset_password_token   :string(255)
reset_password_sent_at :datetime
remember_created_at    :datetime
sign_in_count          :integer(4)      default(0)
current_sign_in_at     :datetime
last_sign_in_at        :datetime
current_sign_in_ip     :string(255)
last_sign_in_ip        :string(255)
failed_attempts        :integer(4)      default(0)
unlock_token           :string(255)
locked_at              :datetime
login                  :string(255)

Instance Method Summary collapse

Instance Method Details

#validate_each(object, attribute, value) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/protected/user.rb', line 32

def validate_each(object, attribute, value)
  if value.present?
    unless value.gsub(" ",'') == value
      object.errors[attribute] << (options[:message] || "cannot contain any whitespace")
    end
    if [value[0], value[-1]].any?{ |x| x == "." }
      object.errors[attribute] << (options[:message] || "cannot contain a period at the start or end")
    end
    unless value =~ /[a-zA-Z]/
      object.errors[attribute] << (options[:message] || "must contain at least one letter")
    end
    unless value =~ /[0-9]/
      object.errors[attribute] << (options[:message] || "must contain at least one number")
    end
    unless value =~ /[_\-.]/
      object.errors[attribute] << (options[:message] || "must contain at least one these special characters \"-_.\"")
    end
    unless value =~ /^[a-zA-Z0-9_\-.]+$/
      object.errors[attribute] << (options[:message] || "can only contain letters number and special characters \"-_.\"")
    end
  end
end