oTree Forum >

Error messages Boolean and multiple choice form fields

#1 by ahallman

Hello! 

I want to write an error message for incorrect answers on Boolean and multiple choice form fields. I get the following error message (in the terminal)  "TypeError: player.q1 is None." and nothing happens in the browser. Submitting the page works anyway. (I want the page to stop and that the errors must be fixed before being able to continue to the next page.) Error messages for IntegerFields works perfectly (if incorrect, message shows up, and the participant has to submit the correct value to be able to proceed to the next page.)

Relevant code: 

class Player(BasePlayer):
    q1 = models.BooleanField(label="question bla bla", choices=[[True, "Alt 1"], [False, "Alt 2"]])
    q4 = models.IntegerField(label="",choices=[[0,"Alt a"],[1,"Alt b"],[2,"Alt c"]])

def q1_error_message(player, value):
    if value == False:
        return 'Your answer is incorrect.'
        
def q4_error_message(player, value):
    if value != 1:
        return 'Your answer is incorrect.'
        
Grateful for help!

#2 by Chris_oTree

Maybe q1 is missing from your form_fields? Anyway, please post your Page class, and the traceback of the error.

#3 by ahallman

Hello again! Sorry, more detailed code, including q2 (IntegerField) that does work. q1(Boolean) & q4 (Alternatives, integer) don't work.  

class Player(BasePlayer):
    q1 = models.BooleanField(label="question bla bla", choices=[[True, "Alt 1"], [False, "Alt 2"]])
    q2 = models.IntegerField(label="")
    q4 = models.IntegerField(label="",choices=[[0,"Alt a"],[1,"Alt b"],[2,"Alt c"]])
    
def q1_error_message(player, value):
    if value == False:
        return 'Your answer is incorrect.'

def q2_error_message(player, value):
    if value != 100:
        return 'Your answer is incorrect. Please enter, in numbers (1234...), the correct answer.'

def q4_error_message(player, value):
    if value != 1:
        return 'Your answer is incorrect.'
        
class instructions(Page):
    form_model = "player"
    form_fields = [
        'q1',
        'q2',
        'q4'
    ]
*****************************************************************'
html-5 page: 
{{ extends 'global/Page.html' }}
{{ block title }}Control questions 1/2{{ endblock }}

{{ block content }}
<p>
bla bla picture. 
{{ formfield 'q1' }}

    {{ formfield 'q2' }}
    <hr>

</p>

<p>
blabla
    {{ formfield 'q4' }}
</p>
    <hr>
{{ next_button }}

<script>
bla bla canvas for picture. 
</script>

{{ endblock }}


*************************************
Error messages in terminal: 
1: If I answer all questions incorrectly, nothing happens in the terminal. 
Error message in browser works correctly for q2. Cannot submit page until submit the correct value. 

2: If I answer q2 correctly, but q1 and q4 incorrectly: Nothing happens in the terminal. Page is submitted. 
The data logged for q1 is 0 (not Boolean!)
q4 stores the correct data (0 or 2 when submitting the wrong value), but the error message does not work as intended. 


******************************************
I'm very confused. Grateful for help!

#4 by Chris_oTree

It works fine for me. Are you using oTree 3 maybe?

#5 by Chris_oTree

By the way, oTree displays boolean values as 0 and 1 in the admin interface, because it's easier to read. 0/1 are essentially the same as False and True.

#6 by ahallman

I'm using oTree 5.8.4. I'm wondering if I need another syntax for the Boolean 

Ah, thank you, I didn't know.

#7 by Chris_oTree

I directly tested your code, and all the error messages display fine for me (including q1 and q4).

Write a reply

Set forum username