learning-unlimited / ESP-Website

A website to help manage the logistics of large, short-term educational programs
82 stars 57 forks source link

Putting a number for school creates an invalid profile state #3746

Closed willgearty closed 2 months ago

willgearty commented 3 months ago

If a student enters a number for the K12 school field, the profile form will submit fine. However, upon trying to submit the profile form again, it will then error with the following message:

K12School matching query does not exist.

My guess is that the form validation guesses that these numeric values correspond to a K12 School object, rather than just a string and then attempts to look up the object (but finds none).

willgearty commented 3 months ago

It's possible this is a broader issue related to AjaxForeignKeyNewformField: https://github.com/learning-unlimited/ESP-Website/blob/b40480728e2eae0d9c42876a2a975df8973e6479/esp/esp/db/forms.py#L156

willgearty commented 3 months ago

Indeed, it looks like the clean function takes this to be an object ID: https://github.com/learning-unlimited/ESP-Website/blob/b40480728e2eae0d9c42876a2a975df8973e6479/esp/esp/db/forms.py#L240-L252

I think the easiest fix here might be to change the front end functionality so that when a number is manually entered, we make it clear that this isn't an autocomplete value: https://github.com/learning-unlimited/ESP-Website/blob/b40480728e2eae0d9c42876a2a975df8973e6479/esp/esp/db/forms.py#L58-L97

Another option (probably easier) is to require that manual values are not only numbers (via regex maybe).

willgearty commented 3 months ago

Actually, I think an even easier change would be to use filter() and exists() here: https://github.com/learning-unlimited/ESP-Website/blob/b40480728e2eae0d9c42876a2a975df8973e6479/esp/esp/db/forms.py#L262-L263

If the object with that ID doesn't exist, then we should just return None (or raise an error if a proper value is required).