I encountered a validation error while trying to create a form with an empty select field:
area_sp = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control', 'id':'area_select'}))
After populating the select in the template via ajax:
$.ajax({
url: '/endpoint/load_areas/',
type: 'post',
dataType: 'json',
data: {
'postcode': postcode
},
headers: {
'X-CSRFToken': "{{ csrf_token }}"
},
success: function (data) {
var ret = JSON.parse(data.result);
for (x in ret) {
$("#area_select").append(new Option(x, ret[x]));
}
},
error: function(data) {
alert("error");
}
});
Upon attempting to submit the form, I received the following error message:
area_sp: Select a valid choice. 26835 is not one of the available choices.
Any suggestions or insights on how to resolve this issue?