From dbd29de6714612450fd6e43e5ee3048f076c7311 Mon Sep 17 00:00:00 2001 From: JG Date: Tue, 12 Jul 2022 14:45:29 +0100 Subject: [PATCH] WIP validations --- app/controllers/schemes_controller.rb | 16 +++++++++++++--- app/models/scheme.rb | 5 ----- app/views/schemes/edit_name.html.erb | 5 ++++- config/locales/en.yml | 17 +++++++++++++++-- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 302ff36cb..12d6aec20 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -27,7 +27,9 @@ class SchemesController < ApplicationController def create @scheme = Scheme.new(scheme_params) - if @scheme.save + validation_errors scheme_params + + if @scheme.errors.empty? && @scheme.save if scheme_params[:support_services_provider] == "The same organisation that owns the housing stock" redirect_to scheme_primary_client_group_path(@scheme) else @@ -44,7 +46,9 @@ class SchemesController < ApplicationController check_answers = params[:scheme][:check_answers] page = params[:scheme][:page] - if @scheme.update(scheme_params) + validation_errors scheme_params + + if @scheme.errors.empty? && @scheme.update(scheme_params) if check_answers if confirm_secondary_page? page redirect_to scheme_secondary_client_group_path(@scheme, check_answers: "true") @@ -56,7 +60,7 @@ class SchemesController < ApplicationController redirect_to next_page_path params[:scheme][:page] end else - render current_template(request.referer), status: :unprocessable_entity + render current_template(page), status: :unprocessable_entity end end @@ -102,6 +106,12 @@ class SchemesController < ApplicationController private + def validation_errors scheme_params + scheme_params.keys.each do |key| + @scheme.errors.add(key.to_sym) if scheme_params[key].to_s.empty? + end + end + def confirm_secondary_page?(page) page == "confirm-secondary" && @scheme.has_other_client_group == "Yes" end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 0eb27a7ea..c0354d62c 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -4,11 +4,6 @@ class Scheme < ApplicationRecord has_many :locations has_many :case_logs - # validates_presence_of :service_name - # validates_presence_of :scheme_type - # validates_presence_of :registered_under_care_act - # validates :primary_client_group, presence: true, on: :update - scope :filter_by_id, ->(id) { where(id: (id.start_with?("S") ? id[1..] : id)) } scope :search_by_service_name, ->(name) { where("service_name ILIKE ?", "%#{name}%") } scope :search_by_postcode, ->(postcode) { joins(:locations).where("locations.postcode ILIKE ?", "%#{postcode.delete(' ')}%") } diff --git a/app/views/schemes/edit_name.html.erb b/app/views/schemes/edit_name.html.erb index 8fd713580..bfccd8ba9 100644 --- a/app/views/schemes/edit_name.html.erb +++ b/app/views/schemes/edit_name.html.erb @@ -28,10 +28,13 @@ label: { text: "This scheme contains confidential information" } %> <% end %> + <% null_option = [OpenStruct.new(id: "", name: "Select an option")] %> <% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %> + <% answer_options = null_option + organisations %> + <% if current_user.support? %> <%= f.govuk_collection_select :owning_organisation_id, - organisations, + answer_options, :id, :name, label: { text: "Which organisation owns the housing stock for this scheme?", size: "m" }, diff --git a/config/locales/en.yml b/config/locales/en.yml index d58e6736f..75f3156ce 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -49,12 +49,25 @@ en: blank: "Enter the existing organisation’s name" service_name: blank: "Enter the scheme’s name" + invalid: "Enter the scheme’s name" scheme_type: blank: "Select the scheme’s type" + invalid: "Select the scheme’s type" registered_under_care_act: - blank: "You must tell us if this scheme is registered under the Care Standards Act 2000" + blank: "Select if this scheme is registered under the Care Standards Act 2000" + invalid: "Select if this scheme is registered under the Care Standards Act 2000" primary_client_group: - blank: "Select what client group is this scheme intended for" + invalid: "Select what client group is this scheme intended for" + secondary_client_group: + invalid: "Select what is the other client group" + support_type: + invalid: "Select level of support given" + intended_stay: + invalid: "Select intended length of stay" + has_other_client_group: + invalid: "Select if this scheme provides for another client group" + support_services_provider: + invalid: "Select who provides the support services used by this scheme" validations: