Browse Source

Validate required fields

pull/677/head
baarkerlounger 3 years ago
parent
commit
728e62645b
  1. 4
      app/controllers/organisations_controller.rb
  2. 3
      app/models/organisation.rb
  3. 4
      config/locales/en.yml
  4. 12
      spec/requests/organisations_controller_spec.rb

4
app/controllers/organisations_controller.rb

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

3
app/models/organisation.rb

@ -18,7 +18,8 @@ class Organisation < ApplicationRecord
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
CaseLog.filter_by_organisation(self)

4
config/locales/en.yml

@ -39,6 +39,10 @@ en:
reset_password: "Reset your password"
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_not_required: "%{other_field_label} must not be provided if %{main_field_label} was not other"
numeric:

12
spec/requests/organisations_controller_spec.rb

@ -1008,6 +1008,18 @@ RSpec.describe OrganisationsController, type: :request do
request
expect(response).to redirect_to("/organisations")
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

Loading…
Cancel
Save