diff --git a/docs/documentation_routes.js b/docs/documentation_routes.js index b0bf6c826e..e9e6767e14 100644 --- a/docs/documentation_routes.js +++ b/docs/documentation_routes.js @@ -45,16 +45,21 @@ router.get('/examples/template-data', function (req, res) { // Branching -router.get('/examples/over-18', function (req, res) { - // get the answer from the query string (eg. ?over18=false) - var over18 = req.query.over18 +router.get('/examples/branch/aliens', function (req, res) { + // get the answer from the query string (eg. ?aliens=2) + // we convert to a number as data in HTTP is always a string + var aliens = Number(req.query.aliens) - if (over18 === 'false') { + if (aliens > 5) { // redirect to the relevant page - res.redirect('/docs/examples/under-18') + res.redirect('/docs/examples/branch/too-many-aliens') + return + } else if (aliens >= 1 && aliens <= 5) { + res.redirect('/docs/examples/branch/some-aliens') + return } else { - // if over18 is any other value (or is missing) render the page requested - res.render('examples/over-18') + // everything looks ok, render the requested page + res.render('examples/branch/aliens') } }) diff --git a/docs/views/examples/branch/aliens.html b/docs/views/examples/branch/aliens.html new file mode 100644 index 0000000000..76727eda95 --- /dev/null +++ b/docs/views/examples/branch/aliens.html @@ -0,0 +1,30 @@ +{% extends "layout.html" %} + +{% block page_title %} + Example - Branching - Some aliens +{% endblock %} + +{% block content %} +
+ + {% include "includes/breadcrumb_examples.html" %} + +
+
+ +

+ No aliens sighted +

+ +

+ This is normal. +

+ +

+ Back to the branching example +

+ +
+
+
+{% endblock %} diff --git a/docs/views/examples/branch/director.html b/docs/views/examples/branch/director.html new file mode 100644 index 0000000000..992dcc6b50 --- /dev/null +++ b/docs/views/examples/branch/director.html @@ -0,0 +1,30 @@ +{% extends "layout.html" %} + +{% block page_title %} + Example - Branching - Director +{% endblock %} + +{% block content %} +
+ + {% include "includes/breadcrumb_examples.html" %} + +
+
+ +

+ Director +

+ +

+ Congratulations, you are a director of a limited company. +

+ +

+ Back to the branching example +

+ +
+
+
+{% endblock %} diff --git a/docs/views/examples/branch/employed.html b/docs/views/examples/branch/employed.html new file mode 100644 index 0000000000..0fbe60d073 --- /dev/null +++ b/docs/views/examples/branch/employed.html @@ -0,0 +1,30 @@ +{% extends "layout.html" %} + +{% block page_title %} + Example - Branching - Employed +{% endblock %} + +{% block content %} +
+ + {% include "includes/breadcrumb_examples.html" %} + +
+
+ +

+ Employed +

+ +

+ Congratulations, you are employed. +

+ +

+ Back to the branching example +

+ +
+
+
+{% endblock %} diff --git a/docs/views/examples/branch/self-employed.html b/docs/views/examples/branch/self-employed.html new file mode 100644 index 0000000000..1742531068 --- /dev/null +++ b/docs/views/examples/branch/self-employed.html @@ -0,0 +1,30 @@ +{% extends "layout.html" %} + +{% block page_title %} + Example - Branching - Self-employed +{% endblock %} + +{% block content %} +
+ + {% include "includes/breadcrumb_examples.html" %} + +
+
+ +

+ Self-employed +

+ +

+ Congratulations, you are self-employed. +

+ +

+ Back to the branching example +

+ +
+
+
+{% endblock %} diff --git a/docs/views/examples/branch/some-aliens.html b/docs/views/examples/branch/some-aliens.html new file mode 100644 index 0000000000..7e9b98d4de --- /dev/null +++ b/docs/views/examples/branch/some-aliens.html @@ -0,0 +1,30 @@ +{% extends "layout.html" %} + +{% block page_title %} + Example - Branching - Some aliens +{% endblock %} + +{% block content %} +
+ + {% include "includes/breadcrumb_examples.html" %} + +
+
+ +

+ Some aliens sighted +

+ +

+ You have seen some aliens, we will send someone to investigate. +

+ +

+ Back to the branching example +

+ +
+
+
+{% endblock %} diff --git a/docs/views/examples/branch/too-many-aliens.html b/docs/views/examples/branch/too-many-aliens.html new file mode 100644 index 0000000000..91a9dce5f0 --- /dev/null +++ b/docs/views/examples/branch/too-many-aliens.html @@ -0,0 +1,30 @@ +{% extends "layout.html" %} + +{% block page_title %} + Example - Branching - Lots of aliens +{% endblock %} + +{% block content %} +
+ + {% include "includes/breadcrumb_examples.html" %} + +
+
+ +

+ Lots of aliens sighted! +

+ +

+ Stay calm, we are sending the army. +

+ +

+ Back to the branching example +

+ +
+
+
+{% endblock %} diff --git a/docs/views/examples/branching.html b/docs/views/examples/branching.html index b986aebd6d..7ff9cc7c4b 100644 --- a/docs/views/examples/branching.html +++ b/docs/views/examples/branching.html @@ -12,8 +12,6 @@
-
-

Branching

@@ -23,41 +21,83 @@

- You can see the code for this example here: + You can make this happen by including , redirect(<url>) after the data in your radio button values. For example:

-
- /docs/documentation_routes.js - /docs/views/examples/branching.html +
+ <div class="multiple-choice"> + <input id="workStatusEmp" type="radio" name="work_status" value="employed, redirect(branch/employed)"> + <label for="workStatusEmp">Employed</label> + </div> + <div class="multiple-choice"> + <input id="workStatusSelf" type="radio" name="work_status" value="selfemployed, redirect(branch/self-employed)"> + <label for="workStatusSelf">Self-employed</label> + </div> + <div class="multiple-choice"> + <input id="workStatusDir" type="radio" name="work_status" value="director, redirect(branch/director)"> + <label for="workStatusDir">Director of a limited company</label> + </div>
-

- Are you 18 or over? -

+ -
-
+
- Are you 18 or over? +

+ What is your employment status? +

-
- - -
+
-
- - -
+ What is your employment status? -
-
+
+ + +
-
- -
+
+ + +
+ +
+ + +
+ +
+
+ +
+ +
+ + + +

 

+ +

Advanced method

+ +

You can write some 'route' code to branch on any kind of input.

+ +

You can see the code for this in /docs/documentation_routes.js

+ +

 

+ +
+ +
+ +

Enter 0, 5 or 20 to see the branching pages

+ +
+ +
+ +
-
+

diff --git a/lib/utils.js b/lib/utils.js index 32b8863760..a6f5dd0b54 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -209,6 +209,27 @@ var storeData = function (input, store) { } } +// check for `redirect()` in submitted form data +// and optionally a value that's been passed too (ie. `redirect(),` ) + +var checkRedirect = function (formdata) { + if (Object.keys(formdata).length !== 0) { + for (var key in formdata) { + if (typeof formdata[key] === 'string') { + var match = formdata[key].match(/([^,]*).*redirect\(([^)]*)\)?/) + if (match !== null) { + // update the data for saving to session + formdata[key] = match[1] || match[2] + + // return the redirect destination url + return match[2] + } + } + } + } + return null +} + // Middleware - store any data sent in session, and pass it to all views exports.autoStoreData = function (req, res, next) { @@ -216,6 +237,8 @@ exports.autoStoreData = function (req, res, next) { req.session.data = {} } + var redirect = checkRedirect(req.query) || checkRedirect(req.body) + storeData(req.body, req.session) storeData(req.query, req.session) @@ -227,5 +250,6 @@ exports.autoStoreData = function (req, res, next) { res.locals.data[j] = req.session.data[j] } - next() + if (redirect) res.redirect(redirect) + else next() }