@@ -11,3 +11,136 @@ Some MongoDB-specific fields are available in ``django_mongodb.forms``.
1111.. class :: ObjectIdField
1212
1313Stores an :class: `~bson.objectid.ObjectId `.
14+
15+ ``SimpleArrayField ``
16+ --------------------
17+
18+ .. class :: SimpleArrayField(base_field, delimiter=',', max_length=None, min_length=None)
19+
20+ A field which maps to an array. It is represented by an HTML ``<input> ``.
21+
22+ .. attribute :: base_field
23+
24+ This is a required argument.
25+
26+ It specifies the underlying form field for the array. This is not used
27+ to render any HTML, but it is used to process the submitted data and
28+ validate it. For example:
29+
30+ .. code-block :: pycon
31+
32+ >>> from django import forms
33+ >>> from django_mongodb.forms import SimpleArrayField
34+
35+ >>> class NumberListForm(forms.Form):
36+ ... numbers = SimpleArrayField(forms.IntegerField())
37+ ...
38+
39+ >>> form = NumberListForm({"numbers": "1,2,3"})
40+ >>> form.is_valid()
41+ True
42+ >>> form.cleaned_data
43+ {'numbers': [1, 2, 3]}
44+
45+ >>> form = NumberListForm({"numbers": "1,2,a"})
46+ >>> form.is_valid()
47+ False
48+
49+ .. attribute :: delimiter
50+
51+ This is an optional argument which defaults to a comma: ``, ``. This
52+ value is used to split the submitted data. It allows you to chain
53+ ``SimpleArrayField `` for multidimensional data:
54+
55+ .. code-block :: pycon
56+
57+ >>> from django import forms
58+ >>> from django_mongodb.forms import SimpleArrayField
59+
60+ >>> class GridForm(forms.Form):
61+ ... places = SimpleArrayField(SimpleArrayField(IntegerField()), delimiter="|")
62+ ...
63+
64+ >>> form = GridForm({"places": "1,2|2,1|4,3"})
65+ >>> form.is_valid()
66+ True
67+ >>> form.cleaned_data
68+ {'places': [[1, 2], [2, 1], [4, 3]]}
69+
70+ .. note ::
71+
72+ The field does not support escaping of the delimiter, so be careful
73+ in cases where the delimiter is a valid character in the underlying
74+ field. The delimiter does not need to be only one character.
75+
76+ .. attribute :: max_length
77+
78+ This is an optional argument which validates that the array does not
79+ exceed the stated length.
80+
81+ .. attribute :: min_length
82+
83+ This is an optional argument which validates that the array reaches at
84+ least the stated length.
85+
86+ .. admonition :: User friendly forms
87+
88+ ``SimpleArrayField `` is not particularly user friendly in most cases,
89+ however it is a useful way to format data from a client-side widget for
90+ submission to the server.
91+
92+ ``SplitArrayField ``
93+ -------------------
94+
95+ .. class :: SplitArrayField(base_field, size, remove_trailing_nulls=False)
96+
97+ This field handles arrays by reproducing the underlying field a fixed
98+ number of times.
99+
100+ .. attribute :: base_field
101+
102+ This is a required argument. It specifies the form field to be
103+ repeated.
104+
105+ .. attribute :: size
106+
107+ This is the fixed number of times the underlying field will be used.
108+
109+ .. attribute :: remove_trailing_nulls
110+
111+ By default, this is set to ``False ``. When ``False ``, each value from
112+ the repeated fields is stored. When set to ``True ``, any trailing
113+ values which are blank will be stripped from the result. If the
114+ underlying field has ``required=True ``, but ``remove_trailing_nulls ``
115+ is ``True ``, then null values are only allowed at the end, and will be
116+ stripped.
117+
118+ Some examples::
119+
120+ SplitArrayField(IntegerField(required=True), size=3, remove_trailing_nulls=False)
121+
122+ ["1", "2", "3"] # -> [1, 2, 3]
123+ ["1", "2", ""] # -> ValidationError - third entry required.
124+ ["1", "", "3"] # -> ValidationError - second entry required.
125+ ["", "2", ""] # -> ValidationError - first and third entries required.
126+
127+ SplitArrayField(IntegerField(required=False), size=3, remove_trailing_nulls=False)
128+
129+ ["1", "2", "3"] # -> [1, 2, 3]
130+ ["1", "2", ""] # -> [1, 2, None]
131+ ["1", "", "3"] # -> [1, None, 3]
132+ ["", "2", ""] # -> [None, 2, None]
133+
134+ SplitArrayField(IntegerField(required=True), size=3, remove_trailing_nulls=True)
135+
136+ ["1", "2", "3"] # -> [1, 2, 3]
137+ ["1", "2", ""] # -> [1, 2]
138+ ["1", "", "3"] # -> ValidationError - second entry required.
139+ ["", "2", ""] # -> ValidationError - first entry required.
140+
141+ SplitArrayField(IntegerField(required=False), size=3, remove_trailing_nulls=True)
142+
143+ ["1", "2", "3"] # -> [1, 2, 3]
144+ ["1", "2", ""] # -> [1, 2]
145+ ["1", "", "3"] # -> [1, None, 3]
146+ ["", "2", ""] # -> [None, 2]
0 commit comments