1 package SL::DB::Helper::IBANValidation;
5 use Algorithm::CheckDigits ();
7 use SL::Locale::String qw(t8);
11 AT => { len => 20, name => t8('Austria') },
12 BE => { len => 16, name => t8('Belgium') },
13 CH => { len => 21, name => t8('Switzerland') },
14 CZ => { len => 24, name => t8('Czech Republic') },
15 DE => { len => 22, name => t8('Germany') },
16 DK => { len => 18, name => t8('Denmark') },
17 FR => { len => 27, name => t8('France') },
18 IT => { len => 27, name => t8('Italy') },
19 LU => { len => 20, name => t8('Luxembourg') },
20 NL => { len => 18, name => t8('Netherlands') },
21 PL => { len => 28, name => t8('Poland') },
25 my ($self, $attribute) = @_;
27 my $iban = $self->$attribute // '';
30 return () unless length($iban);
32 $_validater //= Algorithm::CheckDigits::CheckDigits('iban');
34 return ($::locale->text("The value '#1' is not a valid IBAN.", $iban)) if !$_validater->is_valid($iban);
36 my $country = $_countries{substr($iban, 0, 2)};
38 return () if !$country || (length($iban) == $country->{len});
40 return ($::locale->text("The IBAN '#1' is not valid as IBANs in #2 must be exactly #3 characters long.", $iban, $country->{name}, $country->{len}));
44 my ($package, @attributes) = @_;
46 my $caller_package = caller;
47 @attributes = qw(iban) unless @attributes;
51 *{ $caller_package . '::validate_ibans' } = sub {
54 return map { SL::DB::Helper::IBANValidation::_validate($self, $_) } @attributes;
68 SL::DB::Helper::IBANValidation - Mixin for validating IBAN attributes
72 package SL::DB::SomeObject;
73 use SL::DB::Helper::IBANValidation [ ATTRIBUTES ];
80 push @errors, $self->validate_ibans;
85 This mixin provides a function C<validate_ibans> that returns a list
86 of error messages, one for each attribute that fails the IBAN
87 validation. If all attributes are valid or empty then an empty list
90 The names of attributes to check can be given as an import list to the
91 mixin package. If no attributes are given the single attribute C<iban>
98 =item C<validate_ibans>
100 This function iterates over all configured attributes and validates
101 their content according to the IBAN standard. An attribute that is
102 undefined, empty or consists solely of whitespace is considered valid,
105 The function returns a list of human-readable error messages suitable
106 for use in a general C<validate> function (see SYNOPSIS). For each
107 attribute failing the check the list will include one error message.
109 If all attributes are valid then an empty list is returned.
119 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>