oTree Forum >

Is there an elegant way to capture incorrect form responses?

#1 by mitnec

Hello,
My question is pretty much what it says on the tin. Respondents' incorrect guesses to a particular kind of question may be informative, and I'd like to capture them. Right now we can bounce them with e.g. {field_name}_error_message() if the answer is wrong, but in that case their response is not recorded.

I can think of ways to do this with e.g. many identical fields (guess_1, guess_2) and javascript, but that seems like it's not the best way. I also suppose you could do this with live pages and ExtraModel. But I have the intuition there is a better way.

Any ideas?

#2 by Chris_oTree

Here is an implementation that can store wrong answers for multiple different fields. I set 'response' to be a StringField so that it can flexibly store incorrect responses of any type (bool/string/number).

class Player(BasePlayer):
    myfield = models.IntegerField()


class IncorrectResponse(ExtraModel):
    player = models.Link(Player)
    field_name = models.StringField()
    response = models.StringField() 

def myfield_error_message(player, value):
    if value != 42:
        IncorrectResponse.create(player=player, field_name='myfield', response=str(value))
        return "Wrong answer"

#3 by mitnec

Neat, thank you!

#4 by Chris_oTree

Here is a way to record incorrect answers for multiple fields at once:

    @staticmethod
    def error_message(player: Player, values):
        solutions = dict(quiz1=4, quiz2='Ottawa', quiz3=2019, quiz4=False)

        errors = {f: 'Try again' for f in solutions if values[f] != solutions[f]}
        if errors:
            for field_name in errors:
                response = values[field_name]
                IncorrectResponse.create(player=player, field_name=field_name, response=str(response))
            return errors

Write a reply

Set forum username