diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 1cef5dadb..0eaef3445 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -121,22 +121,12 @@ class LocationsController < ApplicationController day = location_params["startdate(3i)"] month = location_params["startdate(2i)"] year = location_params["startdate(1i)"] - if [day, month, year].none?(&:blank?) - if Date.valid_date?(year.to_i, month.to_i, day.to_i) - @location.startdate = Time.zone.local(year.to_i, month.to_i, day.to_i) - if @location.save(context: :startdate) - redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route]) - else - render :availability, status: :unprocessable_entity - end - else - error_message = I18n.t("validations.location.startdate_invalid") - @location.errors.add :startdate, error_message - render :availability, status: :unprocessable_entity - end + if [day, month, year].none?(&:blank?) && Date.valid_date?(year.to_i, month.to_i, day.to_i) + @location.startdate = Time.zone.local(year.to_i, month.to_i, day.to_i) + end + if @location.save(context: :startdate) + redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route]) else - error_message = I18n.t("validations.location.startdate_blank") - @location.errors.add :startdate, error_message render :availability, status: :unprocessable_entity end end diff --git a/app/models/location.rb b/app/models/location.rb index f8fcbbfb6..d1e6c50d5 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -6,7 +6,8 @@ class Location < ApplicationRecord validates :units, on: :units, presence: { message: I18n.t("validations.location.units") } validates :type_of_unit, on: :type_of_unit, presence: { message: I18n.t("validations.location.type_of_unit") } validates :mobility_type, on: :mobility_type, presence: { message: I18n.t("validations.location.mobility_standards") } - validate :validate_startdate, on: :startdate + validates :startdate, on: :startdate, presence: { message: I18n.t("validations.location.startdate_invalid") } + validate :validate_startdate, on: :startdate, if: proc { |model| model.startdate.presence } validate :validate_confirmed belongs_to :scheme has_many :lettings_logs, class_name: "LettingsLog" @@ -421,8 +422,8 @@ class Location < ApplicationRecord end def validate_startdate - unless startdate.between?(Time.zone.local(1900, 1, 1), Time.zone.local(2200, 1, 1)) - error_message = I18n.t("validations.location.startdate_out_of_range") + unless startdate.between?(scheme.available_from, Time.zone.local(2200, 1, 1)) + error_message = I18n.t("validations.location.startdate_out_of_range", date: scheme.available_from.to_formatted_s(:govuk_date)) errors.add :startdate, error_message end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 9fac1b731..97cd7f899 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -350,9 +350,8 @@ en: units: "The units at this location must be a number" type_of_unit: "Select the most common type of unit at this location" mobility_standards: "Select the mobility standard for the majority of the units at this location" - startdate_blank: "Enter the day, month and year when the first property became available at this location" - startdate_invalid: "Enter a valid day, month and year" - startdate_out_of_range: "Availability date must be between 1900 and 2200" + startdate_invalid: "Enter a valid day, month and year when the first property became available at this location" + startdate_out_of_range: "Availability date must be on or after the %{date}" toggle_date: not_selected: "Select one of the options" invalid: "Enter a valid day, month and year"