Store responses in cache if error message occurs

50 views
Skip to first unread message

Nick S

unread,
Jun 17, 2021, 7:21:22 PM6/17/21
to oTree help & discussion
Hello,

I was wondering what is the best way to store participants initial responses if they fail the form validation condition? Since the form validation refreshes the page it clears all responses and my task is very tedious so I am trying to find a solution.

Cheers,

Nicholas

Chris @ oTree

unread,
Jun 17, 2021, 7:24:15 PM6/17/21
to oTree help & discussion
Can you show your page code? if you're using raw HTML it will lose the data upon reload, but if you use oTree's form rendering, it will save it.

Nick S

unread,
Jun 17, 2021, 7:41:29 PM6/17/21
to oTree help & discussion
Ah that is why. I am using html rendered form fields.

See code below for a snippet:

<table class="table table-striped">
<thead>
<tr>
<th scope="col">Question</th>
<th scope="col">Response (%)</th>
</tr>
</thead>
<tbody>
<tr>
<th>What is the probability you think you got a score of : <strong>0</strong></th>
<td>
<div class="form-group">
<input type="number" class="form-control prc" name="p_score_0" id="p_score0" {% if player.prior_mode_score == 0 %}value="{{ prior_mode_score_probability }}" readonly{% else %}value=0{% endif %}>
</div>
</td>
</tr>
<tr>
<th>What is the probability you think you got a score of : <strong>1</strong></th>
<td>
<div class="form-group">
<input type="number" class="form-control prc" name="p_score_1" id="p_score1" {% if player.prior_mode_score == 1 %}value="{{ prior_mode_score_probability }}" readonly{% else %}value=0{% endif %}>
</div>
</td>
</tr>
</tbody>
</table>

What is the way to do this for html rendered form fields?

Max Spohn

unread,
Jul 9, 2021, 5:56:56 AM7/9/21
to oTree help & discussion
Hi both,

I just came across the same problem, did you find any way to save the data for HTML rendered fields in case of a failed validation? Thanks a lot.

Best,
Max

Chris @ oTree

unread,
Jul 9, 2021, 11:39:42 AM7/9/21
to oTree help & discussion
First, you should use HTML5 validation attributes as much as possible, e.g. 

<input type="number" min="0" max="5" required>

This will not allow the user to submit until their input is valid.

Apart from that, you can use localStorage to store inputs. But yes this is one of the advantages of using {{ formfield }}.

Reply all
Reply to author
Forward
0 new messages