Contact update form bad validation error case
- In store configuration, DE-select "Only sell to in-country customers"; then, under Shipping Countries, only enable a single country.
- The user contact update form (/accounts/update) does not display the 'select country' dropdown box and so the form's 'country' field is empty, causing a "This field is required" error to be generated for the State/Province and ZIP/Postal Code fields.
def clean_state(self): data = self.cleaned_data.get('state') if self._local_only: country = self._default_country else: country = clean_field(self, 'country') if country == None: raise forms.ValidationError(_('This field is required.')) self._check_state(data, country) return data
Validation in this case follows the else clause and finds country == None, even though in the template context, 'country' exists due to this:
init_data['form'] = form if shop.in_country_only: init_data['country'] = shop.sales_country else: countries = shop.countries() if countries and countries.count() == 1: init_data['country'] = countries
init_data is put in the request context (so the country table row w/ dropdown does not show since 'country' == countries, the only available entry)
Ideas to fix:
- in the case of countries.count() == 1 in the view, populate that choice into the initial data of the form. I'm not sure how that would work given the type of field (ModelChoiceField), but I'm sure it is possible
- create a clean_country function on the form instead of using the default. This function could perform the same responsibility - if there is only one country available (and shop.in_country_only == False), supply that country instead of None