oTree Forum >

Conditioning on specific formield errors in a template

#1 by DavidP

Hi,

I would like to condition some parts of my template on the respondent comitting a specific type of validation error.
Meaning I want larger parts of the template to depend on the specific error. For example, I have a custom validation function such as:

def example_error_message(player, value):
    if value == 0:
        return "Error A"
    elif value == 1:
        return "Error B"
    elif value == 2:
        return "Error C"

In the template, I'd like to do something like:

{{ if formfield_errors 'example' == 'Error A' }}
        [some complex code]
{{ endif }}


Is this at all possible? What would the correct syntax be?
(If impossible, I could use a workaround and render the error message in a hidden DOM-Element and then use JavaScript for the conditionals, but that seems hackish and unnecessarily complex, I would hope.)

Thanks!

#2 by BonnEconLab

This seems like a classic use case for live pages, doesn’t it? See https://otree.readthedocs.io/en/latest/live.html.

There is no need to use the built-in error messages. As the JavaScript example code on https://otree.readthedocs.io/en/latest/live.html#example-auction shows, you can set the inner HTML of any element that you like via liveRecv.

#3 by DavidP

@BonnEconLab  Thanks for the suggestion - I suppose it could be done via liveSend, but for a few reasons, not last simplicity, I prefer to use the submission/validation functionality via a POST request that's already in place.

I searched around how this could be done in Django and it appears that way still works in otree. 
Assuming the above example, one can do

{{ if "Error A" in form.errors.example }}
    [ stuff conditional on this specific error ]
{{ endif }}

So, problem solved.

#4 by BonnEconLab

Ah, good to know that one can access the pure text of the error message via {{ form.errors.example }} — or {{ form.errors.example.0 }}, to be precise (the “.0” needs to be added to get rid of the square brackets).

This means that one can apply arbitrary formatting like

<div class="alert alert-danger">
  <b style="font-size: xx-large;">{{ form.errors.example.0 }}</b>
</div>

— whereas {{ formfield_errors 'example' }} comes preformatted.

#5 by DavidP

Yup, good to know!

To briefly document what's going on for posterity: 
form.errors is a dictionary of the form 
{
    "first_field": ["First error message", "Second error message", ... ],
    "second_field":  ["First error message", "Second error message", ... ],
}

Thus, form.errors.example is a list of all errors associated with the example field. The .0 usage is therefore fine, as long as it is assured that there can't be more than one error message per field.

Not sure what happens with validation errors that are not linked to a specific field (https://otree.readthedocs.io/en/latest/forms.html#validating-multiple-fields-together). However, this should be easy to find out by setting up such an error and then including {{ form.errors }} in the template.

Write a reply

Set forum username