Browse Source

testing location validity

pull/760/head
JG 3 years ago
parent
commit
29d25565ac
  1. 5
      app/controllers/locations_controller.rb
  2. 9
      app/models/location.rb

5
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))

9
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")

Loading…
Cancel
Save