kevmhughes / CodeQuestAPI-examen-mf0493_3

This is a REST API to get programming questions
MIT License
0 stars 0 forks source link

Submit New Question: Fix bug when correct answer is not indicated #1

Open kevmhughes opened 1 week ago

kevmhughes commented 1 week ago

Description:

When attempting to submit a new question without selecting a correct answer, a validation message correctly displays an error. However, all form fields are cleared forcing the user to re-enter all the data. The form should retain the user's original input, allowing the user to close the error notification and then simply select the correct answer and resubmit, instead of having to re-enter all the information again.

Screenshot 2024-11-18 at 22 55 41

This screenshot shows the form with all fields cleared after trying to submit a question without a correct answer selected.

Steps to Reproduce:

  1. Access the page: http://localhost:3000/submit-new-question
  2. Fill out the following fields which have the following ids:
    • Question Field: #question
    • Answer 1 Field: #answer1text
    • Answer 2 Field: #answer2text
    • Answer 3 Field: #answer3text
    • Answer 4 Field: #answer4text
  3. Leave all the checkboxes unselected:
    • Checkbox 1: #answer1Checkbox
    • Checkbox 2: #answer2Checkbox
    • Checkbox 3: #answer3Checkbox
    • Checkbox 4: #answer4Checkbox
  4. Submit the form.
Screenshot 2024-11-18 at 22 53 47

This screenshot shows which fields must be filled out to reproduce the unwanted behaviour.

Expected Behaviour:

If no correct answer is selected, on submitting the question an error notification should be displayed. After the message is shown, the user should be able to close the error notification and all previously entered form data (text fields) should remain intact. The user should only need to select the correct answer(s) and resubmit the form.

Actual Behaviour:

When the user submits the question without selecting an answer, all the form fields are cleared forcing the user to re-enter all the data. This behaviour is not expected and disrupts the user experience.

Solution:

The issue arises because the form data is not stored as an object and then passed back to the view after a validation failure. To fix this, we need to ensure that the form is repopulated with the user’s original input after a failed validation.

Steps for a Robust Solution:

Store Form Data on Validation Failure:

Pass Data Back to the View:

Render the Form with the Existing Data:

<textarea name="question"> <%= formInputs && formInputs.question ? formInputs.question : '' %> </textarea>

Things to Take into Consideration:

Ensure Data Integrity:

Avoid Null or Undefined Values:

Consistent Data Passing:

omiras commented 4 days ago

Your description highlights the problem effectively, making it easy to understand the expected behavior and current bug. Great job!