Multiple CHoice

1,690 views
Skip to first unread message

Ruggiero Rippo

unread,
Mar 5, 2021, 12:36:55 PM3/5/21
to oTree help & discussion
Hi community,

in a survey (last part of my experiment) i would like to ask multiple choice question , i.e. allowing the participant to select more than 1 choice. 
So far I thought of this solution, 

appz2 = models.StringField(
label='Che cosa si produce nella sua azienda?',
choices=[['MELE', 'MELE'],['UVA','UVA']],
widget=widgets.RadioSelect,)
appz3 = models.StringField(
label='',
choices=[['UVA DA TAVOLA', 'UVA DA TAVOLA'],['UVA DA VINO','UVA DA VINO']],
widget=widgets.RadioSelect,)

however, is not elegant and a blank space displays between the two fields

any suggestion? I am using otree and pycharm as writer

thx

Chris @ oTree

unread,
Mar 5, 2021, 12:40:10 PM3/5/21
to oTree help & discussion
I recommend a BooleanField(blank=True) for each option, then in the template use a checkbox for each field (<input type="checkbox" ...>, see the docs for more details).
Then the participant can tick all the checkboxes that apply.

Ruggiero Rippo

unread,
Mar 5, 2021, 1:39:01 PM3/5/21
to oTree help & discussion
any hint on the code for bolean?

Rok

unread,
Mar 5, 2021, 4:52:47 PM3/5/21
to oTree help & discussion

https://otree.readthedocs.io/en/latest/forms.html?highlight=checkbox#raw-html-widgets

Player class would look something like this:

class Player(BasePlayer): 
    choice1 = models.BooleanField(blank=True)
    choice2 = models.BooleanField(blank=True)
    # ... etc

Then in your template, you don’t use {% formfields %}, but instead write something like this:

<p>My first multiple choice question:</p>
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" name="choice1">
  <label class="form-check-label">
    Text for choice 1
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" name="choice2">
  <label class="form-check-label">
    Text for choice 2
  </label>
</div>

Since you’re not using {% formfields %} anymore, you can use {% formfield 'my_field' %} (or any other way of doing it from the linked docs above) to display your other input fields on this page.

You may want to include the code {% if form.my_field.errors %}{{ form.my_field.errors.0 }}{% endif %} somewhere on the page. It displays error messages if incorrect user inputs are submitted.

Hope that helps,
Rok

Ruggiero Rippo

unread,
Mar 6, 2021, 3:59:07 PM3/6/21
to oTree help & discussion
Hi Rok,

it works wonderfully. However the answers (the checked boxes) do not appear in the results at the end. Am i missing smtg?

Thank 1000
Ruggiero

Message has been deleted
Message has been deleted
Message has been deleted

Rok

unread,
Mar 7, 2021, 10:18:46 AM3/7/21
to oTree help & discussion

Hello, it seems I left a bit of code out in my original answer.

Make sure you have your BooleanFields set to initial=False, your inputs should have value=”True” and don’t forget to include the fields in your form_fields on the page where you display the checkboxes.

class Player(BasePlayer): 
    choice1 = models.BooleanField(blank=True, initial=False)
    choice2 = models.BooleanField(blank=True, initial=False)
    # ... etc
class MyInputPage(Page):
    form_model = 'player'
    form_fields = ['choice1', 'choice2']
<p>My first multiple choice question:</p>
<div class="form-check">

  <input class="form-check-input" type="checkbox" value="True" name="choice1">

  <label class="form-check-label">
    Text for choice 1
  </label>
</div>
<div class="form-check">

  <input class="form-check-input" type="checkbox" value="True" name="choice2">

  <label class="form-check-label">
    Text for choice 2
  </label>
</div>

Best Regards,
Rok

Rok

unread,
Mar 7, 2021, 10:19:59 AM3/7/21
to oTree help & discussion
Okay I give up with the fancy formatting, it seems to be bugged somehow. Here's the html code from above:

<p>My first multiple choice question:</p>
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="True" name="choice1">
  <label class="form-check-label">
    Text for choice 1
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="True" name="choice2">
  <label class="form-check-label">
    Text for choice 2
  </label>
</div>

Best Regards,
Rok

Valentin

unread,
Sep 20, 2021, 1:25:29 PM9/20/21
to oTree help & discussion
Hi Rok, 

it seems I have a similar problem than ruggier...@unitn.it.

When I use your code, it works quite well as long as I do not add the following error message to the respective page:

def error_message(player, values):
​   if player.choice1 is False and player.choice2 is False:
      return 'Please select at least one answer.'

When I add this error message, the "False" values are not overwritten by "True" values anymore so that the error message shows up even when I select one or both check boxes.

Do you have any idea how to fix this?

Best,
Valentin

Chris @ oTree

unread,
Sep 20, 2021, 4:00:07 PM9/20/21
to oTree help & discussion
This function doesn't work because it doesn't do anything with the 'values' argument, which contains the values the user entered in the form.

Chris @ oTree

unread,
Sep 20, 2021, 4:48:22 PM9/20/21
to oTree help & discussion
Anyway I have updated the example code in otree-snippets so that it shows how to require at least 1 selection (or 2 etc...)

Valentin

unread,
Sep 20, 2021, 4:50:03 PM9/20/21
to oTree help & discussion
Thanks a lot!
Reply all
Reply to author
Forward
0 new messages