Skip to content
Browse files

better email field validation

Users keep hitting problems when they append/prepend whitespace to
the email field (or stick spaces somewhere in the middle).

Until it is possible to take the email from the SSO, this change
strips out all whitespace from requester emails.
  • Loading branch information...
1 parent 1d25024 commit 84b0047350b4a8ef75c6751387ac0dbe0318ad38 @benilovj benilovj committed Feb 11, 2013
Showing with 17 additions and 1 deletion.
  1. +6 −1 app/models/shared/requester.rb
  2. +11 −0 test/unit/models/requester_test.rb
View
7 app/models/shared/requester.rb
@@ -4,10 +4,15 @@ class Requester < TablelessModel
attr_accessor :email
validates_presence_of :email
- validates :email, :format => {:with => /@/}
+
+ validates :email, format: { with: /@/ }
validate :collaborator_emails_are_all_valid
+ def email=(new_email)
+ @email = new_email.nil? ? nil : new_email.gsub("\s", "")
+ end
+
def collaborator_emails
@collaborator_emails || []
end
View
11 test/unit/models/requester_test.rb
@@ -4,13 +4,24 @@ class RequesterTest < Test::Unit::TestCase
should validate_presence_of(:email)
should allow_value("ab@c.com").for(:email)
+ should allow_value("ab@ c.com").for(:email)
+ should allow_value("ab @c.com").for(:email)
+ should allow_value("ab@c.com ").for(:email)
+ should allow_value(" ab@c.com").for(:email)
should_not allow_value("ab").for(:email)
should allow_value("").for(:collaborator_emails)
should allow_value("ab@c.com").for(:collaborator_emails)
should allow_value("ab@c.com, de@f.com").for(:collaborator_emails)
should_not allow_value("ab, de@f.com").for(:collaborator_emails)
+ should "remove all whitespace from the email" do
+ assert_equal "ab@c.com", Requester.new(email: " ab@c.com").email
+ assert_equal "ab@c.com", Requester.new(email: "ab@c.com ").email
+ assert_equal "ab@c.com", Requester.new(email: "ab @c.com").email
+ assert_equal "ab@c.com", Requester.new(email: "ab@ c.com").email
+ end
+
should "have an empty list of collaborator emails if not set" do
assert_equal [], Requester.new.collaborator_emails
end

0 comments on commit 84b0047

Please sign in to comment.
Something went wrong with that request. Please try again.