From a90453e59c3a08af3efbda3a5591937387ed5aac Mon Sep 17 00:00:00 2001 From: simfeld Date: Sat, 9 Mar 2024 19:53:40 +0100 Subject: [PATCH] Implement validation --- app/models/pbs/event/course.rb | 10 ++++++++++ app/views/event/kinds/_fields_pbs.html.haml | 2 +- config/locales/models.pbs.de.yml | 5 +++++ lib/hitobito_pbs/wagon.rb | 2 +- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/models/pbs/event/course.rb b/app/models/pbs/event/course.rb index ccb4cb79e..8b5b8e412 100644 --- a/app/models/pbs/event/course.rb +++ b/app/models/pbs/event/course.rb @@ -13,6 +13,8 @@ module Pbs::Event::Course APPROVALS = %w(requires_approval_abteilung requires_approval_region requires_approval_kantonalverband requires_approval_bund).freeze + COURSE_NUMBER_REGEX = /\APBS CH(?: [A-Z]{2})? [0-9]{3}-[0-9]{2}\z/.freeze + included do include Pbs::Event::RestrictedRole @@ -42,6 +44,14 @@ module Pbs::Event::Course validates :bsv_days, numericality: { greater_than_or_equal_to: 0, allow_blank: true } validate :assert_bsv_days_precision + validates :number, format: { with: COURSE_NUMBER_REGEX, + message: :must_adhere_to_format, + if: :validate_number? } + + def validate_number? + (new_record? || number_changed? || kind_id_changed?) && kind.validate_course_number + end + ### CALLBACKS after_initialize :become_campy before_save :set_requires_approval diff --git a/app/views/event/kinds/_fields_pbs.html.haml b/app/views/event/kinds/_fields_pbs.html.haml index 243649b88..b397b7443 100644 --- a/app/views/event/kinds/_fields_pbs.html.haml +++ b/app/views/event/kinds/_fields_pbs.html.haml @@ -3,4 +3,4 @@ -# or later. See the COPYING file at the top-level directory or at -# https://github.com/hitobito/hitobito_pbs. -= f.labeled_input_fields :documents_text, :campy, :can_have_confirmations, :confirmation_name += f.labeled_input_fields :documents_text, :campy, :can_have_confirmations, :confirmation_name, :validate_course_number diff --git a/config/locales/models.pbs.de.yml b/config/locales/models.pbs.de.yml index d8b46c5aa..1c257be45 100644 --- a/config/locales/models.pbs.de.yml +++ b/config/locales/models.pbs.de.yml @@ -1238,6 +1238,7 @@ de: campy: Kurs als Lager anmelden can_have_confirmations: Export Kursbestätigungen aktivieren confirmation_name: Name der Kursart im Export-Tool + validate_course_number: Kursnummer validieren event/participation: additional_information: Wie möchtest du dich im Anlass ernähren? Was sind deine Essgewohnheiten? @@ -1362,6 +1363,10 @@ de: attributes: base: cannot_remove_parent_id: 'Das übergeordnete Lager kann nicht mehr verlassen werden, da es nicht mehr im Status "Erstellt" ist' + event/course: + attributes: + number: + must_adhere_to_format: muss dem Format "PBS CH KV XXX-JJ" entsprechen person: attributes: prefers_digital_correspondence: diff --git a/lib/hitobito_pbs/wagon.rb b/lib/hitobito_pbs/wagon.rb index c017dcb6b..923d59129 100644 --- a/lib/hitobito_pbs/wagon.rb +++ b/lib/hitobito_pbs/wagon.rb @@ -127,7 +127,7 @@ class Wagon < Rails::Engine :prefers_digital_correspondence] GroupsController.permitted_attrs += [:hostname] Event::KindsController.permitted_attrs += [:documents_text, :campy, :can_have_confirmations, - :confirmation_name] + :confirmation_name, :validate_course_number] QualificationKindsController.permitted_attrs += [:manual] ServiceTokensController.permitted_attrs += [:group_health, :census_evaluations]