Skip to content

Commit

Permalink
Merge pull request #6150 from Raushan998/feature/email_validate_message
Browse files Browse the repository at this point in the history
Feature/email validate message
  • Loading branch information
compwron authored Jan 3, 2025
2 parents e6a9290 + dd285dc commit 64f33ed
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/validators/user_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def validate(record)
valid_phone_number_if_receive_sms_notifications(record)
validate_date_of_birth_in_past(record.date_of_birth, record)
validate_date_of_birth_not_before_1920(record.date_of_birth, record)
validate_uniqueness(:email, record, I18n.t("activerecord.errors.messages.email_uniqueness"))
end

private
Expand Down Expand Up @@ -50,4 +51,15 @@ def validate_date_of_birth_not_before_1920(date_of_birth, record)

record.errors.add(:base, " Date of birth must be on or after 1/1/1920.") unless date_of_birth >= "1920-01-01".to_date
end

def validate_uniqueness(attribute, record, message)
existing_record = record.class.find_by(attribute => record[attribute])

if existing_record && existing_record.id != record.id
record.errors.add(:base, message)
return false
end

true
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ en:
cant_be_future: can't be in the future
must_be_selected: must be selected
must_be_true_or_false: must be true or false
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.
time:
formats:
day_and_date: "%A, %b %d, %Y"
Expand Down
7 changes: 7 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
expect(user.errors[:base]).to eq([" Date of birth must be on or after 1/1/1920."])
end

it "shows custom email uniqueness error message" do
create(:user, email: "[email protected]")
user = build(:user, email: "[email protected]")
expect(user.valid?).to be false
expect(user.errors[:base]).to eq([I18n.t("activerecord.errors.messages.email_uniqueness")])
end

it "has an empty old_emails array when initialized" do
user = build(:user)
expect(user.old_emails).to eq([])
Expand Down

0 comments on commit 64f33ed

Please sign in to comment.