aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Form/Field/Postcode.pm
blob: 093ae66a35013ae52fb187025d3a33a9ca51b1bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package FixMyStreet::App::Form::Field::Postcode;

use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Field::Text';

use mySociety::PostcodeUtil;

apply(
    [
        {
            transform => sub {
                my ( $value, $field ) = @_;
                $value =~ s/[^A-Z0-9]//i;
                return mySociety::PostcodeUtil::canonicalise_postcode($value);
            }
        },
        {
            check => sub { mySociety::PostcodeUtil::is_valid_postcode(shift) },
            message => 'Sorry, we did not recognise that postcode.',
        }
    ]
);


__PACKAGE__->meta->make_immutable;
use namespace::autoclean;

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

FixMyStreet::App::Form::Field::Postcode - validates postcode using mySociety::PostcodeUtil

=head1 DESCRIPTION

Validates that the input looks like a postcode using L<mySociety::PostcodeUtil>.
Widget type is 'text'.

=head1 DEPENDENCIES

L<mySociety::PostcodeUtil>

=cut