Opened 7 years ago

Closed 7 years ago

#8720 closed (invalid)

MultipleHiddenInput doesn't give list of elements in clean method

Reported by: stefan+django@… Owned by: nobody
Component: Forms Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


When cleaning a MultipleHiddenInput field, the data is a unicode string of a python representation of a list, so like this u'[u'123', u'456']' which makes it kind of hard to validate the data. I think it should be a proper list, if i'm not missing something here.

Change History (6)

comment:1 Changed 7 years ago by kratorius

  • milestone set to 1.0
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

I cannot reproduce the error. If you think this is a bug in django, could you please provide a test case or an example?

comment:2 Changed 7 years ago by kyjv

  • Resolution worksforme deleted
  • Status changed from closed to reopened

Ok, I set up a test project with recent django(r8955).
It has a simple view that renders a form with four hidden inout fields with the name testfield.
When submitted, the values will be sent back to the view and form.is_valid is called. The form looks like this:

from django import forms

class TestForm(forms.Form):
    testfield = forms.CharField(required=False, widget=forms.MultipleHiddenInput())

    def clean_testfield(self):
        print self.cleaned_data.get('testfield')
        print self.cleaned_data.get('testfield')[0]
        return self.cleaned_data.get('testfield')

This will print [u'1', u'2', u'3', u'4'] and the character [

I expected it to give me u'1' when I access it as a list (like a list of unicode characters should behave)

If I'm just doing something wrong, please tell me.

comment:3 Changed 7 years ago by mtredinnick

  • milestone 1.0 deleted

Too late for 1.0 now.

comment:4 Changed 7 years ago by kyjv

Well, that depends if you consider that a bug or not. Parsing that string doesn't make sense to me at all.
But anyway, it should just be fixed at some point.

comment:5 Changed 7 years ago by kratorius

I've gone a bit further with this. The problem is that a CharField can't handle multiple fields, and will always evaluate those to a string representation (due to a smart_unicode conversion). I guess in this case you just need to use MultipleChoiceField or a custom field.

I'm not sure whether this should be fixed or not, as my point of view is that if you're using multiple stuff, CharField is not the field for you but I'm leaving this open so someone else could look at this.

comment:6 Changed 7 years ago by jacob

  • Resolution set to invalid
  • Status changed from reopened to closed

Yeah, you can't just arbitrarily use any widget with any field and accept it to work. You want a MultipleChoiceField if you're using any sort of multiple widget.

Note: See TracTickets for help on using tickets.
Back to Top