oTree Studio: display form fields differently for different participants in the experiment

1.4k Views Asked by At

I am new to oTree and oTree Studio, so I apologise in advance if my question doesn't make much sense. I am also new to Stack Overflow, so I apologise if I am duplicate a question.

I am writing an application for a lab experiment using oTree Studio. During that experiment, participants are given a series of “opportunities” to exchange resources in the form of “points” having monetary value. The opportunities are organised into rounds and, in a given round, each participant can share her resources with up to 2 other participants. Importantly, a participant cannot transfer her resources to herself; the resources can only be transferred to another participant.

Now, in each round, all the participants in the experiment, except for the given one, are listed on the screen. The participant is then asked, for each of the other subjects, if she wants to transfer her resources to that participant. To that end, I have created an integer field form for each participant which takes value of 1 if the participant transfers her resources to the particular recipient and 0 (the default value) otherwise. The point is that the form field corresponding to the given actor is hidden to keep the participant from transferring the resources to herself. I did it as follows:

{% if player.id_in_group != 1 %} {% formfield player.ile1 %} {% else %}     {% endif %}

The problem, however, is that once the participants completes the screen and clicks on “Next”, an error message is displayed saying

Please fix the errors in the form.

One can try to avoid the problem by allowing “empty” forms, so that the hidden form field corresponding to the given participant can be left unfilled. But then I cannot impose additional restrictions, such as limiting the number of potential recipients to 2. I mean I have defined the following error message for participants:

if values['ile1'] + values['ile2'] + ... > 2: return 'The maximum number of recipients exceeded.'

But then the application fails and displays an error message saying

unsupported operand type(s) for +: 'NoneType' and 'int'

So, I guess, what I am looking for is a way to combine these two things: (a) allowing for the form fields to be left unfilled and, at the same time, (b) applying a restriction on the maximum number of recipients. Does anybody know how to do this?

1

There are 1 best solutions below

0
Chris On

Use get_form_fields to show different fields depending on self.player.id_in_group: https://otree.readthedocs.io/en/latest/forms.html#determining-form-fields-dynamically