diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index a2624a790..156f1cd97 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -16,7 +16,10 @@ class LocationsController < ApplicationController end def create - @location = Location.new(location_params) + @location = if Date.valid_date?(location_params["startdate(3i)"].to_i, location_params["startdate(2i)"].to_i, location_params["startdate(1i)"].to_i) + Location.new(location_params.except("startdate(3i)", "startdate(2i)", "startdate(1i)")) + else + Location.new(location_params) end if @location.save location_params[:add_another_location] == "Yes" ? redirect_to(new_location_path(id: @scheme.id)) : redirect_to(scheme_check_answers_path(scheme_id: @scheme.id)) diff --git a/app/models/location.rb b/app/models/location.rb index 0d5bf1a76..7200b9081 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -1,5 +1,5 @@ class Location < ApplicationRecord - validate :validate_postcode + validate :validate_postcode, :validate_startdate validates :units, :type_of_unit, presence: true belongs_to :scheme @@ -49,6 +49,13 @@ private PIO = PostcodeService.new + def validate_startdate + unless startdate.nil? || (startdate.is_a?(ActiveSupport::TimeWithZone) && !startdate.year.zero?) + error_message = I18n.t("validations.date.invalid_date") + errors.add :startdate, error_message + end + end + def validate_postcode if postcode.nil? || !postcode&.match(POSTCODE_REGEXP) error_message = I18n.t("validations.postcode")