Skip to content

Commit

Permalink
revert pass dat
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-rose28 committed Jul 6, 2023
1 parent 9466b4f commit fb7f079
Showing 1 changed file with 99 additions and 23 deletions.
122 changes: 99 additions & 23 deletions docs/v13/documentation/pass-data.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
---
heading: Pass data from page to page
---
When a user answers questions in your prototype, the kit stores their answers in session data. You can use or show their stored answers later.

[View an example of what passing data looks like in a prototype](./examples/pass-data/vehicle-registration).

To clear the data (for example at the end of a user research session) use the **Clear data** link in the footer.
The kit stores data from all answers that users give in a prototype. This lets you use or show the answers later.

> To take users from one page to another, your button must have a `form` tag. Do not add a `href` - this will turn the button into a link and means it will not submit any data.
To clear the data (for example at the end of a user research session) use the **Clear data** link in the footer.

Find out how to:
- [show data in check your answers](./make-first-prototype/show-users-answers)
- [set default data](set-data)
- [show previous answers when a user goes back to a page](pass-answers)
- [use an answer to change the content on a page](conditional-content)
[View an example of what passing data looks like in a prototype](./examples/pass-data/vehicle-registration).

## How to use

The kit stores data from answers that users give in a prototype using the `name` attribute of the input. We can then use the `name` attribute to `call` this session data later on.

Names cannot have spaces, so use hyphens and lowercase for simplicity.
The kit stores data from answers that users give in a prototype using the `name` attribute of the input.

For example, when a user enters their first name you could have this input:

Expand All @@ -37,34 +29,106 @@ You can show what the user entered later on like this:
</p>
```

## Checkboxes and radios
### Show answers in inputs

If a user goes back to a page where they entered data, they would expect to see the answer they gave.

Most inputs use the `value` option:

```
{{ govukInput({
name: 'first-name',
value: data['first-name']
}) }}
```

For checkboxes the option is `values`, since more than one can be selected.

To identify each radio or checkbox answer (like the country name) we use the `value` tag. For a text input, the `value` is whatever the user enters in the text area.
### Show content if users have not answered optional questions

For example, we’ve used `name: "country"` to set the list of countries:
You can show content if data is currently blank using `or`, for example:

```
{{ govukRadios({
name: "country"
{{ data['first-name'] or "First name not given" }}
```

Then set each country as a `value`:
You can also use this in a component. For example to show that a user has not answered an optional question on Check your answers:

```
items: [
{
value: "england",
text: "England"
},
{{ govukSummaryList({
rows: [
{
key: {
text: "First name"
},
value: {
text: data['first-name'] or "First name not given"
}
}
]
})}}
```


### Set default data

You can set default data in your prototype. This will appear without the user having to enter anything.

For example, if you want to test a journey where the user returns to review or update their information.

If the user changes this data in the prototype, their new answers are saved.

Add default data to your `app/data/session-data-defaults.js` file.

For example, to set default data for the ‘First name’ and ‘Over 18’ inputs in the [passing data example](https://prototype-kit.service.gov.uk/docs/examples/pass-data/vehicle-registration) add:

```
module.exports = {
'first-name': 'Amina',
'over-18': 'yes'
}
```

### Use links to set data

You can use links to set data. If you want to test different scenarios, you can have links to set data for each scenario.

To set data from a link, add a `?` to the `href` followed by the data you want to set:

```
<a href="/start?first-name=Amina">
```

To set more than one piece of data in a link, use an `&` between them:

```
<a href="/start?first-name=Amina&over-18=yes">
```

If the user changes this data in the prototype, their new answers will be saved.

## Advanced features

### Checkboxes and radios using HTML

If you are using the HTML components instead of Nunjucks, you need to use the `checked` function for radios and checkboxes. For example:

```
<input class="govuk-checkboxes__input" id="waste-2" name="waste" type="checkbox" value="mines" {{ checked('waste','mines') }}>
```

### Use the data on the server

You can access the data on the server in a route function.

For example for an input with `name="first-name"`:

```
var firstName = req.session.data['first-name']
```

### Nested values

For complex data you can use nested values, for example:
Expand Down Expand Up @@ -97,3 +161,15 @@ You can access the data on the server in a route function:
```
var firstName = req.session.data['claimant']['first-name']
```

### Ignore inputs

To prevent an input being stored, use an underscore at the start of the name.

```
{{ govukInput({
name: '_secret'
}) }}
```

To use this data you'll have to [write your own route](create-routes).

0 comments on commit fb7f079

Please sign in to comment.