Skip to content

Commit

Permalink
Implement validation
Browse files Browse the repository at this point in the history
  • Loading branch information
simfeld committed Mar 9, 2024
1 parent 4a7cc8f commit a90453e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
10 changes: 10 additions & 0 deletions app/models/pbs/event/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/event/kinds/_fields_pbs.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions config/locales/models.pbs.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/hitobito_pbs/wagon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit a90453e

Please sign in to comment.