diff options
author | Zarino Zappia <mail@zarino.co.uk> | 2017-07-18 16:42:49 +0100 |
---|---|---|
committer | Zarino Zappia <mail@zarino.co.uk> | 2017-07-28 14:35:48 +0100 |
commit | 405cf67716a5d6ec835973d7093e3a71f9b52879 (patch) | |
tree | 3f3a993eb8c34c955b8dc2bd84a066369460ca13 | |
parent | 9d5e44842fdac0d3894ed82885a4079df85ba890 (diff) |
Option to redirect to custom URL from Contact form
If a `success_url` parameter is provided along with the contact form
fields, the user will be redirected to that URL upon successful form
submission.
We use it for the first time on the (UK) FixMyStreet Pro marketing page.
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Contact.pm | 22 | ||||
-rw-r--r-- | t/app/controller/contact.t | 35 | ||||
-rw-r--r-- | templates/web/fixmystreet.com/about/council.html | 10 |
3 files changed, 63 insertions, 4 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Contact.pm b/perllib/FixMyStreet/App/Controller/Contact.pm index e1eb64ba3..f2c3be47c 100644 --- a/perllib/FixMyStreet/App/Controller/Contact.pm +++ b/perllib/FixMyStreet/App/Controller/Contact.pm @@ -54,7 +54,8 @@ sub submit : Path('submit') : Args(0) { && $c->forward('determine_contact_type') && $c->forward('validate') && $c->forward('prepare_params_for_email') - && $c->forward('send_email'); + && $c->forward('send_email') + && $c->forward('redirect_on_success'); } =head2 determine_contact_type @@ -99,7 +100,7 @@ sub determine_contact_type : Private { =head2 validate -Validate the form submission parameters. Sets error messages and redirect +Validate the form submission parameters. Sets error messages and redirect to index page if errors. =cut @@ -262,6 +263,23 @@ sub send_email : Private { return 1; } +=head2 redirect_on_success + +Redirect to a custom URL if one was provided + +=cut + +sub redirect_on_success : Private { + my ( $self, $c ) = @_; + + if (my $success_url = $c->get_param('success_url')) { + $c->res->redirect($success_url); + $c->detach; + } + + return 1; +} + =head1 AUTHOR Struan Donald diff --git a/t/app/controller/contact.t b/t/app/controller/contact.t index 0e87eb4cc..c1039d15b 100644 --- a/t/app/controller/contact.t +++ b/t/app/controller/contact.t @@ -380,6 +380,41 @@ for my $test ( }; } +for my $test ( + { + fields => { + em => 'test@example.com', + name => 'A name', + subject => 'A subject', + message => 'A message', + dest => 'from_council', + success_url => '/faq', + }, + url_should_be => 'http://localhost/faq', + }, + { + fields => { + em => 'test@example.com', + name => 'A name', + subject => 'A subject', + message => 'A message', + dest => 'from_council', + success_url => 'http://www.example.com', + }, + url_should_be => 'http://www.example.com', + }, + ) +{ + subtest 'check user can be redirected to a custom URL after contact form is submitted' => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'fixmystreet' ], + }, sub { + $mech->post('/contact/submit', $test->{fields}); + is $mech->uri->as_string, $test->{url_should_be}; + } + }; +} + $problem_main->delete; done_testing(); diff --git a/templates/web/fixmystreet.com/about/council.html b/templates/web/fixmystreet.com/about/council.html index 93c2604d1..b6690a6d2 100644 --- a/templates/web/fixmystreet.com/about/council.html +++ b/templates/web/fixmystreet.com/about/council.html @@ -25,7 +25,7 @@ </div> <div class="councils-hero__demo-access"> <h2>Try FixMyStreet Professional right now, on our demo site</h2> - <form class="councils-hero__demo-access__form js-fms-pro-demo-form"> + <form action="/contact/submit" method="post" class="councils-hero__demo-access__form js-fms-pro-demo-form"> <div class="form-group"> <label for="demo-name">Name</label> <span class="required">required</span> @@ -43,9 +43,15 @@ </div> <div class="form-group"> <label for="demo-job">Job title</label> - <input type="text" name="job" id="demo-job"> + <input type="text" name="extra.job_title" id="demo-job"> </div> <div class="form-group submit-group"> + <input type="hidden" name="extra.referer" value="[% c.req.headers.referer | html %]"> + <input type="hidden" name="subject" value="Demo site request"> + <input type="hidden" name="message" value="This visitor was sent a link to demo.fixmystreet.com"> + <input type="hidden" name="recipient" value="enquiries"> + <input type="hidden" name="dest" value="from_council"> + <input type="hidden" name="success_url" value="https://demo.fixmystreet.com"> <input type="submit" value="Let me in" class="btn"> </div> </form> |