From 634f92b8a663983a336054d872f60a95f24f80ca Mon Sep 17 00:00:00 2001 From: Serena Abbott <serena.abbott@education.gov.uk> Date: Tue, 10 Dec 2024 14:31:20 +0000 Subject: [PATCH] Add validation to evaluators form --- app/models/support/evaluator.rb | 3 +-- config/locales/validation/support/en.yml | 12 ++++++++++-- spec/models/support/evaluator_spec.rb | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/models/support/evaluator.rb b/app/models/support/evaluator.rb index b7337a204..7ee49559f 100644 --- a/app/models/support/evaluator.rb +++ b/app/models/support/evaluator.rb @@ -2,10 +2,9 @@ module Support class Evaluator < ApplicationRecord belongs_to :support_case, class_name: "Support::Case" - include HasNormalizedEmail - validates :email, presence: true, + format: { with: URI::MailTo::EMAIL_REGEXP }, uniqueness: { case_sensitive: false, scope: :support_case_id } validates :first_name, :last_name, presence: true diff --git a/config/locales/validation/support/en.yml b/config/locales/validation/support/en.yml index b73d548f1..de3aa69af 100644 --- a/config/locales/validation/support/en.yml +++ b/config/locales/validation/support/en.yml @@ -7,6 +7,16 @@ en: attributes: body: blank: Please enter a case note + support/evaluator: + attributes: + email: + blank: Enter the email address of the evaluator + invalid: Enter an email address in the correct format, like name@example.com + taken: This email address has already been added as an evaluator + first_name: + blank: Enter the first name of the evaluator + last_name: + blank: Enter the last name of the evaluator activemodel: errors: @@ -65,8 +75,6 @@ en: blank: Select files to upload has_uploaded_documents: blank: Please confirm that you uploaded all documents - - forms: rules: diff --git a/spec/models/support/evaluator_spec.rb b/spec/models/support/evaluator_spec.rb index 5cb169bc0..1c6b4c0e8 100644 --- a/spec/models/support/evaluator_spec.rb +++ b/spec/models/support/evaluator_spec.rb @@ -23,5 +23,19 @@ evaluator.support_case = case_b expect(evaluator).to be_valid end + + it "doesn't allow invalid email formats" do + support_case = create(:support_case) + evaluator = build(:support_evaluator, support_case:) + + evaluator.email = "test.example.com" + expect(evaluator).not_to be_valid + + evaluator.email = "@example.com" + expect(evaluator).not_to be_valid + + evaluator.email = "example" + expect(evaluator).not_to be_valid + end end end