#1 by martin
Hi everyone, I have a StringField where I require a minimum response length. When the length is not met, an error message pops up, and the participant needs to respond again. The problem is that the originally input text disappears, and the participant has to start over. Is there any way to retain the original response? I include the main pieces of the code: class Player(BasePlayer): write = models.StringField() def write_error_message(player, value): if len(value) < 10: return "You must write a longer response." class Survey0(Page): form_model = 'player' form_fields = ['write'] {{ block content }} <b>Can you explain the reasons for making these decisions?</b> {{ formfield 'write' label=''}} <button class="otree-btn-next">Next</button> {{ endblock }}
#2
by
BonnEconLab
(edited )
I just tested your minimal working example (using oTree 5.11.1). And ... it works. ;-) That is, it works as you would like it to work: the original input *is* retained if it is less than 10 characters long and the error message appears. If I instead use the manual coding <div> <input type="text" name="write" id="id_write" required> {{ formfield_errors "write" }} </div> then the input is *not* retained when it is less than 10 characters long and the error message appears. But as long as I use {{ formfield "write" label="" }} everything is fine. Also using the standard `{{ formfields }}` works as you would desire. (As a matter of fact, if you include both variants in your HTML template, you can observe that upon submission of an input that is too short, the value from the former is transferred to the latter, showing that oTree sends it back to the browser upon reloading the page after the erroneous submission.)
#3 by martin
Thanks for the response! I was actually using <textarea class="form-control" rows="7" name="write"></textarea> to better control the aesthetics, so your explanation helped me figured out that was the problem.