oTree Forum >

Change 'Please fix the errors.' message for form errors

#1 by simoncolumbus

I am using the IncorrectResponse(ExtraModel) method to capture incorrect form responses: https://www.otreehub.com/forum/116/

Using this method, I can set an error message which will be presented underneath each incorrect form field. However, the method also display a generic 'Please fix the errors.' message at the top, which appears to be hardcoded into the Page.html template. Is there a way to modify this message, either globally or on a specific page?

#2 by Chris_oTree

def error_message(player, values):
    return "fix your mistakes"
    
if you need to return a dict (because you have separate error messages for each field), you can return a key called '__all__':

def error_message(player, values):
    return dict(__all__="fix your mistakes", field1="wrong answer")

#3 by simoncolumbus

Thanks Chris, this is very useful. However, that just changes the error message under the respective form field, but not the one on top of the page (unless I implemented it incorrectly). See the attached screenshot -- I would like to change the 'Please fix the errors.' message at the top.

Just in case it's relevant, code below.

class IncorrectResponse(ExtraModel):
    player = models.Link(Player)
    field_name = models.StringField()
    response = models.StringField() 
    
class Comprehension_Check(Page):
    form_model = 'player'
    form_fields = ['comprehensionb_1', #'comprehensionb_2', 
                   'comprehensionb_2',
                   'comprehensionb_3', 
                   'comprehensionb_4', 
                   'comprehensionb_5']
    @staticmethod
    def error_message(player: Player, values):
        solutions = dict(comprehensionb_1=1,
                         comprehensionb_2=2,
                         comprehensionb_3=1, 
                         comprehensionb_4=2, 
                         comprehensionb_5=1)
        
        errors = {f: 'Wrong answer! Please 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

#4 by Chris_oTree

Never mind, the __all__ technique I mentioned is not currently implemented.
Instead, you can use CSS to hide the red banner, and then use {{ if form.errors }} ... {{ endif }} to display some custom content instead.

Write a reply

Set forum username