Skip to content

Commit 64f33ed

Browse files
authored
Merge pull request #6150 from Raushan998/feature/email_validate_message
Feature/email validate message
2 parents e6a9290 + dd285dc commit 64f33ed

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

app/validators/user_validator.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def validate(record)
88
valid_phone_number_if_receive_sms_notifications(record)
99
validate_date_of_birth_in_past(record.date_of_birth, record)
1010
validate_date_of_birth_not_before_1920(record.date_of_birth, record)
11+
validate_uniqueness(:email, record, I18n.t("activerecord.errors.messages.email_uniqueness"))
1112
end
1213

1314
private
@@ -50,4 +51,15 @@ def validate_date_of_birth_not_before_1920(date_of_birth, record)
5051

5152
record.errors.add(:base, " Date of birth must be on or after 1/1/1920.") unless date_of_birth >= "1920-01-01".to_date
5253
end
54+
55+
def validate_uniqueness(attribute, record, message)
56+
existing_record = record.class.find_by(attribute => record[attribute])
57+
58+
if existing_record && existing_record.id != record.id
59+
record.errors.add(:base, message)
60+
return false
61+
end
62+
63+
true
64+
end
5365
end

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ en:
6060
cant_be_future: can't be in the future
6161
must_be_selected: must be selected
6262
must_be_true_or_false: must be true or false
63+
email_uniqueness: This email is already in use. If it does not appear on your roster, it may be associated with another CASA organization. Please use a different email address.
6364
time:
6465
formats:
6566
day_and_date: "%A, %b %d, %Y"

spec/models/user_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@
5555
expect(user.errors[:base]).to eq([" Date of birth must be on or after 1/1/1920."])
5656
end
5757

58+
it "shows custom email uniqueness error message" do
59+
create(:user, email: "[email protected]")
60+
user = build(:user, email: "[email protected]")
61+
expect(user.valid?).to be false
62+
expect(user.errors[:base]).to eq([I18n.t("activerecord.errors.messages.email_uniqueness")])
63+
end
64+
5865
it "has an empty old_emails array when initialized" do
5966
user = build(:user)
6067
expect(user.old_emails).to eq([])

0 commit comments

Comments
 (0)