Skip to content

Commit 6b3c273

Browse files
committedMar 11, 2015
Added Formset.prototype.prefixFormat
Added API docs for prefixFormat. Closes #70
1 parent 9bb0ac0 commit 6b3c273

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed
 

‎CHANGES.md

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
* Added new core `Field` argument: `field.widgetAttrs` - this allows you to
2020
provide additional widget attributes without having to redefine the entire
2121
widget ([#62](https://github.com/insin/newforms/issues/62))
22+
* Added `Form.prototype.prefixFormat` (`'{prefix}-{name}'`) and
23+
`FormSet.prototype.prefixFormat` (`'{prefix}-{index}'`) to define how prefixes
24+
are generated. These can be overridden when extending these components to
25+
customise now field names are generated when prefixes are used
26+
([#70](https://github.com/insin/newforms/issues/70))
2227

2328
## Changes
2429

‎docs/forms_api.rst

+12
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ Forms API
138138
:param Boolean kwargs.emptyPermitted:
139139
if ``true``, the form is allowed to be empty -- defaults to ``false``.
140140

141+
**Prototype Properties**
142+
143+
.. js:attribute:: Form#prefixFormat
144+
145+
This string defines the format used to generate ``name`` attributes for
146+
fields when a form instance is given a ``prefix``. It must contain
147+
``{prefix}`` and ``{name}`` placeholders.
148+
149+
The default format is ``'{prefix}-{name}'``.
150+
151+
:type String:
152+
141153
**Instance Properties**
142154

143155
Form options documented in ``kwargs`` above are all set as instance

‎docs/formsets_api.rst

+12
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ Formsets API
116116
:param Object constructorProps:
117117
properties to be set directly on the new constructor function.
118118

119+
**Prototype Properties**
120+
121+
.. js:attribute:: FormSet#prefixFormat
122+
123+
This string defines the format used to generate a ``prefix`` for forms in
124+
the formset to ensure they have unique ``name`` attributes. It must
125+
contain ``{prefix}`` and ``{index}`` placeholders.
126+
127+
The default format is ``'{prefix}-{index}'``.
128+
129+
:type String:
130+
119131
**Instance Properties**
120132

121133
Formset options documented in ``kwargs`` above are set as instance properties.

‎src/FormSet.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var Concur = require('Concur')
44
var getFormData = require('get-form-data')
55
var is = require('isomorph/is')
6+
var {formatObj} = require('isomorph/format')
67
var object = require('isomorph/object')
78

89
var constants = require('./constants')
@@ -61,6 +62,8 @@ var ManagementForm = (function() {
6162
* @param {Object=} kwargs
6263
*/
6364
var FormSet = Concur.extend({
65+
prefixFormat: '{prefix}-{index}',
66+
6467
constructor: function FormSet(kwargs) {
6568
// TODO Perform PropType checks on kwargs in development mode
6669
kwargs = object.extend({
@@ -897,7 +900,7 @@ FormSet.prototype.nonFormPending = function() {
897900
* @param {Number} index the index of a form in the formset.
898901
*/
899902
FormSet.prototype.addPrefix = function(index) {
900-
return this.prefix + '-' + index
903+
return formatObj(this.prefixFormat, {prefix: this.prefix, index: index})
901904
}
902905

903906
FormSet.prototype.getDefaultPrefix = function() {

‎tests/formsets-server.js

+28
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,34 @@ QUnit.test("Basic FormSet", 5, function() {
8080
strictEqual(formset.hasChanged(), false)
8181
})
8282

83+
QUnit.test('Custom prefix format', function() {
84+
// By default, formsets append a hyphen between the prefix and theform index,
85+
// but a formset can alter that behaviour by overriding prefixFormat, which
86+
// should be a string containing {prefix} and {index} placeholders.
87+
var ChoiceForm = forms.Form.extend({
88+
prefixFormat: '{prefix}[{name}]',
89+
choice: forms.CharField(),
90+
votes: forms.IntegerField()
91+
})
92+
93+
var ChoiceFormSet = forms.FormSet.extend({
94+
form: ChoiceForm,
95+
prefixFormat: '{prefix}[{index}]'
96+
})
97+
98+
var initial = [{choice: "Calexico", votes: 100}]
99+
var formset = new ChoiceFormSet({initial: initial, autoId: false, prefix: 'choices'})
100+
reactHTMLEqual(renderAll(formset.forms()),
101+
'<div>\
102+
<div>Choice: <input type="text" name="choices[0][choice]" value="Calexico"></div>\
103+
<div>Votes: <input type="number" name="choices[0][votes]" value="100"></div>\
104+
</div>\
105+
<div>\
106+
<div>Choice: <input type="text" name="choices[1][choice]"></div>\
107+
<div>Votes: <input type="number" name="choices[1][votes]"></div>\
108+
</div>')
109+
})
110+
83111
QUnit.test("Formset validation", 2, function() {
84112
// FormSet instances can also have an error attribute if validation failed for
85113
// any of the forms.

0 commit comments

Comments
 (0)
Please sign in to comment.