diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 32a135e36..fb1f2f074 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -121,7 +121,9 @@ private @resource.errors.add :role, I18n.t("validations.role.invalid") end - if user_params[:phone].present? && !valid_phone_number?(user_params[:phone]) + if user_params[:phone].blank? + @resource.errors.add :phone, :blank + elsif !valid_phone_number?(user_params[:phone]) @resource.errors.add :phone end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 73cf91d0a..df75770f9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -156,6 +156,7 @@ en: taken: "Enter an email address that hasn’t already been used to sign up" phone: invalid: "Enter a telephone number in the correct format" + blank: "Enter a telephone number" role: invalid: "Role must be data accessor, data provider or data coordinator" blank: "Select role" diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index 8a491f2ca..ce5bba1db 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -850,6 +850,7 @@ RSpec.describe UsersController, type: :request do name: "new user ", email: "new_user@example.com", role: "data_coordinator", + phone: "12345678910", }, } end @@ -1312,6 +1313,7 @@ RSpec.describe UsersController, type: :request do expect(page).to have_field("user[name]") expect(page).to have_field("user[email]") expect(page).to have_field("user[role]") + expect(page).to have_field("user[phone]") end it "allows setting the role to `support`" do @@ -1610,6 +1612,7 @@ RSpec.describe UsersController, type: :request do name: "new user", email:, role: "data_coordinator", + phone: "12345612456", organisation_id: organisation.id, }, } @@ -1641,6 +1644,7 @@ RSpec.describe UsersController, type: :request do name: "", email: "", role: "", + phone: "", organisation_id: nil, }, } @@ -1656,6 +1660,7 @@ RSpec.describe UsersController, type: :request do expect(page).to have_content(I18n.t("activerecord.errors.models.user.attributes.name.blank")) expect(page).to have_content(I18n.t("activerecord.errors.models.user.attributes.email.blank")) expect(page).to have_content(I18n.t("activerecord.errors.models.user.attributes.organisation_id.blank")) + expect(page).to have_content(I18n.t("activerecord.errors.models.user.attributes.phone.blank")) end end