Cancelling prodigyanswer event

Hi there,

I just want to ask if there is any way I can cancel the event that gets triggered when user clicks answer button. I've got a use case in prodigy where I need to validate if the selected labels have the correct datatype before storing them in db. I realize that I can do the validation part by subscribing to prodigyanswer event but I was wondering if there's a way to prevent prodigy from moving to the next screen in case if validation fails?

Hi! From what you describe, it sounds like the validate_answer callback would be the best fit here: https://prodi.gy/docs/custom-recipes#validate_answer

It lets you define a Python function that runs on an example as it's submitted, and you can then raise custom errors if the example is invalid. The user will then see an alert and won't be able to submit the answer until it passes validation. (See here for an example of how this looks.)

1 Like

Thanks @ines, I'm always amazed by your quick response. I tried the solution and it works pretty well. Unfortunately, I also have few custom html controls that are taking input from users and I was hoping if there was way to validate them too. I understand that validate_answer only takes prodigy spans as input param and can't handle validation of custom control values.

The validate_answer callback receives the whole JSON object, so if your custom fields write to the task object (e.g. by calling window.prodigy.update in JavaScript on change events), you'll get all of your custom values for validation :slightly_smiling_face:

1 Like

Yup, this one did the job. Looks like validate_answer runs before prodigyanswer event gets fired, so you can block the user if validation fails. Thanks @ines