Permalink
Browse files

Merge pull request #48 from alphagov/better_email_field_validation

better email field validation
  • Loading branch information...
2 parents bc855d5 + 84b0047 commit bc043847621059aa75083299eca7ee0790433b43 @jamiecobbett jamiecobbett committed Feb 12, 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
@@ -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
@@ -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 bc04384

Please sign in to comment.