Typeaheads are generally used in two ways:
- Choosing predictions is optional
- Choosing predictions is compulsory
You donโt need to handle errors if predictions are optional, but you have to handle errors if predictions are compulsory.
When predictions are optional
You want consider what happens if the GET request fails.
If predictions are optional, users can type what they want into the input field manually. Itโs not a big deal if predictions donโt show up.
For example, try going offline before typing into the search form on Google. Google doesnโt show any predictions.
When prediction are compulsory
Airline websites are great examples where this happen.
Thereโs a code for each airport. Like SIN or WSSS for Singaporeโs airport. Airlines websites probably need one of these airport codes in the backend. (Iโm not sure which one though).
Airlines need to gather the correct airport code from what users select, so they need users to select a prediction.
Hereโs an example: I typed the correct countries into the โFromโ and โToโ fields. But the website showed me an error. This happened even though thereโs only one prediction!
There are two ways you can handle these types of errors:
- Check the input in the backend (assuming you have a backend) and refresh the page with an error.
- Check the input and create an error message. (This uses Ajax).
Either way works.
Error handling for the Typeahead
Predictions are optional for our Typeahead, so we wonโt need to show any errors.
Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson