oTree Forum >

Error message if two values inputted in form field are different

#1 by otree

Hi, 
How can I define an error message that appears if the two values inputted by the respondent in two different form fields in the same page are not equal to each other?

Basically, I want to make sure the person inputs the correct number. If she/he inputs two different numbers, I want the error message to appear. 

Thanks a lot!

#2 by BonnEconLab

This is addressed on https://otree.readthedocs.io/en/latest/forms.html?highlight=error_message#validating-multiple-fields-together, isn’t it?

Adapting the code from that page for your purpose yields:

class MyPage(Page):

    form_model = 'player'
    form_fields = ['number1', 'number2']

    @staticmethod
    def error_message(player, values):
        if values['number1'] != values['number2']:
            return 'The two numbers are not the same.'

Write a reply

Set forum username