#1
by
Tedquestion
Hi Chris and all,
I am trying to figure out a way to show error messages when participants answer two questions that contradict to each other. For now, I have figured out a way to show an error message for a single question. For example, if a participant answered "A" for a question that requires an answer "B," a real-time message as below shows:
def quiz1_error_message(player, value):
print('value is', value)
if value != 'T':
return "Your answer is incorrect. Generating a positive return is important because Oval uses the returns to help fund operations in subsequent years. Please try again."
I am trying to expand the above coding to "two questions" as in, if one answered Question 1 AND Question 2 in a contradicting way, show a real-time message. So, if a participant answered Question 1 AND Question 2 both A, assuming this is a contradicting response, there is an error message to fix the response.
Does anyone have any idea how to solve this issue?
Thanks in advance!
#2
by
Chris_oTree
Use the error_message function on the page: https://otree.readthedocs.io/en/latest/forms.html#validating-multiple-fields-together
@staticmethod
def error_message(player, values):
if values['quiz1'] == 'A' and values['quiz2'] == 'A':
return "This is a contradictory response"
This will show the error message at the top of the page. If you want to show these messages inline, return a dict:
@staticmethod
def error_message(player, values):
if values['quiz1'] == 'A' and values['quiz2'] == 'A':
return dict(quiz1="Error message for field 1", quiz2="Error message for field 2")
#3
by
Tedquestion
Works perfect! Thanks, Chris :)