Issue #1452 resolved

Contact update form bad validation error case

Darren Hollenbeck
created an issue

To reproduce:

  1. In store configuration, DE-select "Only sell to in-country customers"; then, under Shipping Countries, only enable a single country.
  2. 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.

.../satchmo/apps/satchmo_store/contact/forms.py

    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:

.../satchmo/apps/satchmo_store/contact/views.py

    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[0]

init_data is put in the request context (so the country table row w/ dropdown does not show since 'country' == countries[0], the only available entry)

Ideas to fix:

  1. 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
  2. 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

Comments (3)

  1. Darren Hollenbeck reporter

    Can't think of any off-hand, and it has been working fine for me.

    Possibly a minor user annoyance due to having an extra visible form field that isn't required? Not something I'd see as significant, but who knows... people like to complain about strange things.

  2. Log in to comment