Browse Source

Validate required fields when creating Organisations (#677)

* Validate required fields

* Model test

* Rubocop
pull/678/head v0.1.23
baarkerlounger 3 years ago committed by GitHub
parent
commit
794b09d605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/controllers/organisations_controller.rb
  2. 3
      app/models/organisation.rb
  3. 4
      config/locales/en.yml
  4. 2
      spec/models/organisation_spec.rb
  5. 12
      spec/requests/organisations_controller_spec.rb

4
app/controllers/organisations_controller.rb

@ -49,8 +49,8 @@ class OrganisationsController < ApplicationController
end end
def create def create
organisation = Organisation.create(org_params) @resource = Organisation.new(org_params)
if organisation.persisted? if @resource.save
redirect_to organisations_path redirect_to organisations_path
else else
render :new, status: :unprocessable_entity render :new, status: :unprocessable_entity

3
app/models/organisation.rb

@ -18,7 +18,8 @@ class Organisation < ApplicationRecord
enum provider_type: PROVIDER_TYPE enum provider_type: PROVIDER_TYPE
validates :provider_type, presence: true validates :name, presence: { message: I18n.t("validations.organisation.name_missing") }
validates :provider_type, presence: { message: I18n.t("validations.organisation.provider_type_missing") }
def case_logs def case_logs
CaseLog.filter_by_organisation(self) CaseLog.filter_by_organisation(self)

4
config/locales/en.yml

@ -39,6 +39,10 @@ en:
reset_password: "Reset your password" reset_password: "Reset your password"
validations: validations:
organisation:
name_missing: "Enter the organisation’s name"
provider_type_missing: "Select the organisation’s type"
other_field_missing: "If %{main_field_label} is other then %{other_field_label} must be provided" other_field_missing: "If %{main_field_label} is other then %{other_field_label} must be provided"
other_field_not_required: "%{other_field_label} must not be provided if %{main_field_label} was not other" other_field_not_required: "%{other_field_label} must not be provided if %{main_field_label} was not other"
numeric: numeric:

2
spec/models/organisation_spec.rb

@ -20,7 +20,7 @@ RSpec.describe Organisation, type: :model do
it "validates provider_type presence" do it "validates provider_type presence" do
expect { FactoryBot.create(:organisation, provider_type: nil) } expect { FactoryBot.create(:organisation, provider_type: nil) }
.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Provider type can’t be blank") .to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Provider type #{I18n.t('validations.organisation.provider_type_missing')}")
end end
context "with data protection confirmations" do context "with data protection confirmations" do

12
spec/requests/organisations_controller_spec.rb

@ -1008,6 +1008,18 @@ RSpec.describe OrganisationsController, type: :request do
request request
expect(response).to redirect_to("/organisations") expect(response).to redirect_to("/organisations")
end end
context "when required params are missing" do
let(:name) { "" }
let(:provider_type) { "" }
it "displays the form with an error message" do
request
expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("validations.organisation.name_missing"))
expect(page).to have_content(I18n.t("validations.organisation.provider_type_missing"))
end
end
end end
end end
end end

Loading…
Cancel
Save