aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Form/Field/Postcode.pm
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/App/Form/Field/Postcode.pm')
-rw-r--r--perllib/FixMyStreet/App/Form/Field/Postcode.pm50
1 files changed, 50 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/App/Form/Field/Postcode.pm b/perllib/FixMyStreet/App/Form/Field/Postcode.pm
new file mode 100644
index 000000000..093ae66a3
--- /dev/null
+++ b/perllib/FixMyStreet/App/Form/Field/Postcode.pm
@@ -0,0 +1,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
+