Hi all,

I'm hoping to validate a quiz response before the user/student submits it. I have a function that will check the syntax of the answer, for example, and if there are trivial errors I want to raise an error and cause the submit() to not go through.

It appears that form_set_error should do this, and that I should be able to put it in a _validate function for the form ID that contains the form element I want to validate. It almost works; I can tell that the _validate function is being called, but the quiz answer still seems to get submit()'ed and the error CSS class gets applied to the textarea on the next problem in the quiz.

The desired behavior is for the _validate function to cause submit() to not happen, and the CSS error highlight to appear on the page where the validation error occurred.

Comments

sokrplare’s picture

I wonder if you can pick up insights by looking at the short_answer module (quiz/question_types/short_answer) as it must do something very similar I'd imagine. Or are you approaching this all with JS/AJAX? Not sure how else it would be done pre-submit.

djdevin’s picture

Version: 7.x-4.0-alpha9 » 7.x-5.x-dev
Component: Code - other » Code - Quiz core
Issue summary: View changes
Status: Active » Closed (fixed)

Just for anyone searching this issue...

Quiz 5 uses the Form API so "quiz_question_answering_form" is the form ID you would want to alter. That form handles answer submission for one or more questions.

In Quiz 4 this is not possible as the form is submitted through $_POST and does not go through the form submission logic that Drupal provides.

Throwing any sort of validation errors in quiz_question_answering_form will now behave the same way as any other form in Drupal.