diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-04-19 18:24:32 +0100 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-04-23 15:39:51 +0100 |
commit | 41a5f14388e92fa9dbbb5dc3a72d9ad83291eeb4 (patch) | |
tree | 8c374383eb4adc94fa4066291d4563f8444bcb5e | |
parent | 76c8fe259443c88d644b738d6b88e054d03c3078 (diff) |
Add way of validating data in report_form_extras.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 8 | ||||
-rw-r--r-- | t/cobrand/form_extras.t | 35 |
3 files changed, 40 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 563c491db..900addd20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ - Add HTML email previewer. - Add CORS header to Open311 output. #2022 - Add some Cypress browser-based testing. + - Add validation to cobrand-specific custom reporting fields. * v2.3.1 (12th February 2018) - Front end improvements: diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index 7468b0041..8c6c1b244 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -966,6 +966,14 @@ sub process_report : Private { my $value = $c->get_param($form_name) || ''; $c->stash->{field_errors}->{$form_name} = _('This information is required') if $field->{required} && !$value; + if ($field->{validator}) { + eval { + $value = $field->{validator}->($value); + }; + if ($@) { + $c->stash->{field_errors}->{$form_name} = $@; + } + } $report->set_extra_metadata( $form_name => $value ); } diff --git a/t/cobrand/form_extras.t b/t/cobrand/form_extras.t index 84ded5bc1..df76ccbe1 100644 --- a/t/cobrand/form_extras.t +++ b/t/cobrand/form_extras.t @@ -2,7 +2,10 @@ package FixMyStreet::Cobrand::Tester; use parent 'FixMyStreet::Cobrand::FixMyStreet'; sub report_form_extras { - ( { name => 'address', required => 1 }, { name => 'passport', required => 0 } ) + ( + { name => 'address', required => 1 }, + { name => 'passport', required => 0, validator => sub { die "Invalid number\n" if $_[0] && $_[0] !~ /^P/; return $_[0] } }, + ) } # To allow a testing template override @@ -30,6 +33,7 @@ FixMyStreet::override_config { $mech->get_ok('/around'); $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } }, "submit location" ); $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" ); + $mech->submit_form_ok( { button => 'submit_register', with_fields => { @@ -38,13 +42,24 @@ FixMyStreet::override_config { name => 'Joe Bloggs', may_show_name => '1', username => 'test-1@example.com', - passport => '123456', password_register => '', } }, - "submit details without address, with passport", + "submit details without address or passport", ); $mech->content_like(qr{<label for="form_address">Address</label>\s*<p class='form-error'>This information is required</p>}, 'Address is required'); + $mech->content_lacks("<p class='form-error'>Invalid number", 'Passport is optional'); + + $mech->submit_form_ok( { + button => 'submit_register', + with_fields => { + passport => '123456', + } + }, + "submit details with bad passport", + ); + $mech->content_like(qr{<label for="form_address">Address</label>\s*<p class='form-error'>This information is required</p>}, 'Address is required'); + $mech->content_like(qr{<p class='form-error'>Invalid number}, 'Passport format wrong'); $mech->content_contains('value="123456" name="passport"', 'Passport number reshown'); $mech->submit_form_ok( { @@ -55,11 +70,23 @@ FixMyStreet::override_config { }, "submit details, now with address", ); + $mech->content_lacks('This information is required', 'Address is present'); + $mech->content_like(qr{<p class='form-error'>Invalid number}, 'Passport format wrong'); + $mech->content_contains('value="123456" name="passport"', 'Passport number reshown'); + + $mech->submit_form_ok( { + button => 'submit_register', + with_fields => { + passport => 'P123456', + } + }, + "submit details with correct passport", + ); $mech->content_contains('Now check your email'); my $problem = FixMyStreet::DB->resultset('Problem')->search({}, { order_by => '-id' })->first; is $problem->get_extra_metadata('address'), 'My address', 'Address is stored'; - is $problem->get_extra_metadata('passport'), '123456', 'Passport number is stored'; + is $problem->get_extra_metadata('passport'), 'P123456', 'Passport number is stored'; }; END { |