-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.js
More file actions
1048 lines (953 loc) · 29.6 KB
/
forms.js
File metadata and controls
1048 lines (953 loc) · 29.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileOverview Forms, which manage input and validation of input using a
* collection of Fields.
*/
var util = require('util');
var widgets = require('widgets');
var fields = require('fields');
var extendObject = util.extendObject;
var ValidationError = util.ValidationError;
var Field = fields.Field;
var FileField = fields.FileField;
var DOMBuilder = require('DOMBuilder').DOMBuilder;
/**
* Converts "firstName" and "first_name" to "First name", and
* "SHOUTING_LIKE_THIS" to "SHOUTING LIKE THIS".
*/
var prettyName = exports.prettyName = (function()
{
var capsRE = /([A-Z]+)/g;
var splitRE = /[ _]+/;
var trimRE = /(^ +| +$)/g;
var allCapsRE = /^[A-Z][A-Z0-9]+$/;
return function(name)
{
// Prefix sequences of caps with spaces and split on all space
// characters.
var parts = name.replace(capsRE, " $1").split(splitRE);
// If we had an initial cap...
if (parts[0] === "")
{
parts.splice(0, 1);
}
// Give the first word an initial cap and all subsequent words an
// initial lowercase if not all caps.
for (var i = 0, l = parts.length; i < l; i++)
{
if (i == 0)
{
parts[0] = parts[0].charAt(0).toUpperCase() +
parts[0].substr(1);
}
else if (!allCapsRE.test(parts[i]))
{
parts[i] = parts[i].charAt(0).toLowerCase() +
parts[i].substr(1);
}
}
return parts.join(" ");
};
})();
/**
* A field and its associated data.
*
* @param {Form} form a form.
* @param {Field} field one of the form's fields.
* @param {String} name the name under which the field is held in the form.
* @constructor
*/
var BoundField = exports.BoundField = function (form, field, name)
{
this.form = form;
this.field = field;
this.name = name;
this.htmlName = form.addPrefix(name);
if (this.field.label !== null)
{
this.label = this.field.label;
}
else
{
this.label = prettyName(name);
}
this.helpText = field.helpText || "";
}
BoundField.prototype =
{
get errors()
{
return this.form.errors[this.name] || new this.form.errorConstructor();
},
get isHidden()
{
return this.field.widget.isHidden;
},
/**
* Calculates and returns the <code>id</code> attribute for this BoundFIeld if
* the associated form has an autoId.
*/
get autoId()
{
var autoId = this.form.autoId;
if (autoId)
{
autoId = ""+autoId;
if (autoId.indexOf("{0}") != -1)
{
return autoId.format(this.htmlName);
}
else
{
return this.htmlName;
}
}
return "";
},
get data()
{
return this.field.widget.valueFromData(this.form.data,
this.form.files,
this.htmlName);
}
};
/**
* Renders a widget for the field.
*
* @param {Object} [kwargs] configuration options
* @config {Widget} [widget] an override for the widget used to render the field
* - if not provided, the field's configured widget
* will be used
* @config {Object} [attrs] additional attributes to be added to the field's
* widget.
*/
BoundField.prototype.asWidget = function(kwargs)
{
kwargs = extendObject({widget: null, attrs: null}, kwargs || {});
var widget = (kwargs.widget !== null ? kwargs.widget : this.field.widget);
var attrs = kwargs.attrs || {};
var autoId = this.autoId;
if (autoId &&
typeof attrs.id == "undefined" &&
typeof widget.attrs.id == "undefined")
{
attrs.id = autoId;
}
var data;
if (!this.form.isBound)
{
if (typeof this.form.initial[this.name] !== "undefined")
{
data = this.form.initial[this.name];
}
else
{
data = this.field.initial;
}
if (typeof(data) == "function")
{
data = data();
}
}
else
{
data = this.data;
}
return widget.render(this.htmlName, data, attrs);
};
/**
* Renders the field as a hidden field.
*
* @param {Object} [attrs] additional attributes to be added to the field's
* widget.
*/
BoundField.prototype.asHidden = function(attrs)
{
return this.asWidget({widget: new this.field.hiddenWidget(),
attrs: attrs || {}});
};
/**
* Renders the field as a text input.
*
* @param {Object} [attrs] additional attributes to be added to the field's
* widget.
*/
BoundField.prototype.asText = function(attrs)
{
return this.asWidget({widget: new widgets.TextInput(),
attrs: attrs || {}});
};
/**
* Renders the field as a textarea.
*
* @param {Object} [attrs] additional attributes to be added to the field's
* widget.
*/
BoundField.prototype.asTextarea = function(attrs)
{
return this.asWidget({widget: new widgets.Textarea(),
attrs: attrs || {}});
};
/**
* Creates an HTML <code><label></code> for the field.
*
* @param {Object} [kwargs] configuration options.
* @config {String} [contents] contents for the label - if not provided, label
* contents will be generated from the field itself.
* @config {Object} [attrs] additional attributes to be added to the label.
*/
BoundField.prototype.labelTag = function(kwargs)
{
kwargs = extendObject({contents: null, attrs: null}, kwargs || {});
var contents;
if (kwargs.contents !== null)
{
contents = kwargs.contents;
}
else
{
contents = this.label;
}
var widget = this.field.widget;
var id;
if (typeof widget.attrs.id != "undefined")
{
id = widget.attrs.id;
}
else
{
id = this.autoId;
}
if (id)
{
var attrs = extendObject(kwargs.attrs || {},
{"for": widget.idForLabel(id)});
contents = DOMBuilder.createElement("label", attrs, [contents]);
}
return contents;
};
/**
* Assuming this method will only be used when DOMBuilder is configured to
* generate HTML.
*/
BoundField.prototype.toString = function()
{
return ""+this.asWidget();
};
BoundField.prototype.toString.safe = true;
/**
* A collection of Fields that knows how to validate and display itself.
*
* @param {Object} [kwargs] configuration options.
* @config {Object} [data] input form data, where property names are field
* names.
* @config {Object} [files] input file data - this is meaningless on the
* client-side, but is included for future use in any
* future server-side implementation.
* @config {String} [autoId] a template for use when automatically generating
* <code>id</code> attributes for fields, which should
* contain a <code>"{0}"</code> placeholder for
* the field name - defaults to
* <code>"id_{0}"</code>.
* @config {String} [prefix] a prefix to be applied to the name of each field in
* this instance of the form - using a prefix allows
* you to easily work with multiple instances of the
* same Form object in the same HTML
* <code><form></code>, or to safely mix Form
* objects which have fields with the same names.
* @config {Object} [initial] initial form data, where property names are field
* names - if a field's value is not specified in
* <code>data</code>, these values will be used when
* rendering field widgets.
* @config {Function} [errorConstructor] the constructor function to be used
* when creating error details - defaults
* to {@link ErrorList}.
* @config {String} [labelSuffix] a suffix to be used when generating labels
* in one of the convenience method which renders
* the entire Form - defaults to
* <code>":"</code>.
* @config {Boolean} [emptyPermitted] if <code>true</code>, the form is allowed
* to be empty - defaults to
* <code>false</code>.
* @constructor
*/
var Form = exports.Form = function (kwargs)
{
kwargs = extendObject({
data: null, files: null, autoId: "id_{0}", prefix: null, initial: null,
errorConstructor: util.ErrorList, labelSuffix: ":", emptyPermitted: false
}, kwargs || {});
this.isBound = kwargs.data !== null || kwargs.files !== null;
this.data = kwargs.data || {};
this.files = kwargs.files || {};
this.autoId = kwargs.autoId;
this.prefix = kwargs.prefix;
this.initial = kwargs.initial || {};
this.errorConstructor = kwargs.errorConstructor;
this.labelSuffix = kwargs.labelSuffix;
this.emptyPermitted = kwargs.emptyPermitted;
this._errors = null; // Stores errors after clean() has been called
this._changedData = null;
this.fields = extendObject({}, this.fields || {});
}
/** Property under which non-field-specific errors are stored. */
Form.NON_FIELD_ERRORS = '__all__';
Form.prototype =
{
/**
* Getter for errors, which first cleans the form if there are no errors
* defined yet.
*
* @return errors for the data provided for the form.
* @type ErrorObject
*/
get errors()
{
if (this._errors === null)
{
this.fullClean();
}
return this._errors;
},
get changedData()
{
if (this._changedData === null)
{
this._changedData = [];
// XXX: For now we're asking the individual fields whether or not
// the data has changed. It would probably be more efficient to hash
// the initial data, store it in a hidden field, and compare a hash
// of the submitted data, but we'd need a way to easily get the
// string value for a given field. Right now, that logic is embedded
// in the render method of each field's widget.
for (var name in this.fields)
{
if (!this.fields.hasOwnProperty(name))
{
continue;
}
var field = this.fields[name];
var prefixedName = this.addPrefix(name);
var dataValue = field.widget.valueFromData(this.data,
this.files,
prefixedName);
var initialValue;
if (typeof this.initial[name] != "undefined")
{
initialValue = this.initial[name];
}
else
{
initialValue = field.initial;
}
if (field._hasChanged(initialValue, dataValue))
{
this._changedData.push(name);
}
}
}
return this._changedData;
}
// TODO get media()
};
// TODO Come up with a suitable replacement for __iter__
//def __iter__(self):
// for name, field in self.fields.items():
// yield BoundField(self, field, name)
/* The yield keyword is only available in Firefox - adding the necessary
;version=1.7 to the script tag breaks other browsers, so leave be for now.
Form.prototype.__iterator__ = function()
{
var fields = [];
for (var name in this.fields)
{
if (this.fields.hasOwnProperty(name))
{
fields[fields.length] =
new BoundField(this, this.fields[name], name);
}
}
fields.sort(function(a, b)
{
return a.field.creationCounter - b.field.creationCounter;
});
for (var i = 0, l = fields.length; i < l; i++)
{
yield fields[i];
}
};
*/
Form.prototype.toString = function()
{
return ""+this.defaultRendering();
};
Form.prototype.toString.safe = true;
Form.prototype.defaultRendering = function()
{
return this.asTable();
};
/**
* Creates a {@link BoundField} for the field with the given name.
*
* @param {String} name a field name.
*
* @return a <code>BoundField</code> for the field with the given name, if one
* exists.
* @type BoundField
*/
Form.prototype.boundField = function(name)
{
if (!this.fields.hasOwnProperty(name))
{
throw new Error("Form does not have a " + name + " field.");
}
return new BoundField(this, this.fields[name], name);
};
Form.prototype.getTemplateVariable = function (name)
{
return (this.fields.hasOwnProperty(name)
? new BoundField(this, this.fields[name], name)
: this[name]);
};
/**
* Creates a {@link BoundField} for each field in the form, ordering them
* by the order in which the fields were created.
*
* @param {Function} [test] if provided, this function will be called with
* "field" and "name" arguments - BoundFields will
* only be generated for fields for which
* <code>true</code> is returned.
*
* @return a list of <code>BoundField</code> objects - one for each field in
* the form, in the order in which the fields were created.
* @type Array
*/
Form.prototype.boundFields = function(test)
{
test = test || function() { return true; };
var fields = [];
for (var name in this.fields)
{
if (this.fields.hasOwnProperty(name) &&
test(this.fields[name], name) === true)
{
fields.push(new BoundField(this, this.fields[name], name));
}
}
return fields;
};
/**
* Determines whether or not the form has errors.
*
* @return <code>true</code> if the form has no errors, <code>false</code>
* otherwise. If errors are being ignored, returns <code>false</code>.
* @type Boolean
*/
Form.prototype.isValid = function()
{
if (!this.isBound)
{
return false;
}
return !this.errors.isPopulated();
};
/**
* Returns the field name with a prefix appended, if this Form has a prefix set.
*
* @param {String} fieldName a field name.
*
* @return a field name with a prefix appended, if this Form has a prefix set,
* otherwise <code>fieldName</code> is returned as-is.
* @type String
*/
Form.prototype.addPrefix = function(fieldName)
{
if (this.prefix !== null)
{
return util.formatString("%(prefix)s-%(fieldName)s",
{prefix: this.prefix, fieldName: fieldName});
}
return fieldName;
};
// TODO Form.addInitialPrefix
/**
* Helper function for outputting HTML.
*
* @param {Function} normalRow a function which produces a normal row.
* @param {Function} errorRow a function which produces an error row.
* @param {Boolean} errorsOnSeparateRow determines if errors are placed in their
* own row, or in the row for the field
* they are related to.
* @param {Boolean} [doNotCoerce] if <code>true</code>, the resulting rows will
* not be coerced to a String if we're operating
* in HTML mode - defaults to <code>false</code>.
*
* @return if we're operating in DOM mode returns a list of DOM elements
* representing rows, otherwise returns an HTML string, with rows
* separated by linebreaks.
*/
Form.prototype._htmlOutput = function(normalRow, errorRow, errorsOnSeparateRow, doNotCoerce)
{
var topErrors = this.nonFieldErrors();
var rows = []
var hiddenFields = [];
var hiddenBoundFields = this.hiddenFields();
for (var i = 0, l = hiddenBoundFields.length; i < l; i++)
{
var bf = hiddenBoundFields[i];
var bfErrors = bf.errors;
if (bfErrors.isPopulated())
{
for (var j = 0, m = bfErrors.errors.length; j < m; j++)
{
topErrors.errors.push("(Hidden field " + bf.name + ") " +
bfErrors.errors[j]);
}
}
hiddenFields.push(bf.asWidget());
}
var visibleBoundFields = this.visibleFields();
for (var i = 0, l = visibleBoundFields.length; i < l; i++)
{
var bf = visibleBoundFields[i];
// Variables which can be optional in each row
var errors = null, label = null, helpText = null, extraContent = null;
var bfErrors = bf.errors;
if (bfErrors.isPopulated())
{
errors = new this.errorConstructor();
for (var j = 0, m = bfErrors.errors.length; j < m; j++)
{
errors.errors.push(bfErrors.errors[j]);
}
if (errorsOnSeparateRow === true)
{
rows.push(errorRow(errors.defaultRendering()));
errors = null;
}
}
if (bf.label)
{
var isSafe = DOMBuilder.isSafe(bf.label);
label = ""+bf.label;
// Only add the suffix if the label does not end in punctuation
if (this.labelSuffix &&
":?.!".indexOf(label.charAt(label.length - 1)) == -1)
{
label += this.labelSuffix;
}
if (isSafe)
{
label = DOMBuilder.markSafe(label);
}
label = bf.labelTag({contents: label}) || "";
}
if (bf.field.helpText)
{
helpText = bf.field.helpText;
}
// If this is the last row, it should include any hidden fields
if (i == l - 1 && hiddenFields.length > 0)
{
extraContent = hiddenFields;
}
if (errors !== null)
{
errors = errors.defaultRendering();
}
rows.push(normalRow(label, bf.asWidget(), helpText, errors, extraContent));
}
if (topErrors.isPopulated())
{
// Add hidden fields to the top error row if it's being displayed and
// there are no other rows.
var extraContent = null;
if (hiddenFields.length > 0 && rows.length == 0)
{
extraContent = hiddenFields;
}
rows.splice(0, 0, errorRow(topErrors.defaultRendering(), extraContent));
}
// Put hidden fields in their own error row if there were no rows to
// display.
if (hiddenFields.length > 0 && rows.length == 0)
{
rows.push(errorRow("", hiddenFields));
}
if (doNotCoerce === true || DOMBuilder.mode == "DOM")
{
return rows;
}
else
{
return util.ak.safe(rows.join("\n"));
}
};
/**
* Returns this form rendered as HTML <tr>s - excluding the
* <table></table>.
*
* @param {Boolean} [doNotCoerce] if <code>true</code>, the resulting rows will
* not be coerced to a String if we're operating
* in HTML mode - defaults to <code>false</code>.
*/
Form.prototype.asTable = function(doNotCoerce)
{
var normalRow = function(label, field, helpText, errors, extraContent)
{
var contents = [];
if (errors)
{
contents.push(errors);
}
contents.push(field);
if (helpText)
{
contents.push(DOMBuilder.createElement("br"));
contents.push(helpText);
}
if (extraContent)
{
contents = contents.concat(extraContent);
}
return DOMBuilder.createElement("tr", {}, [
DOMBuilder.createElement("th", {}, [label]),
DOMBuilder.createElement("td", {}, contents)
]);
};
var errorRow = function(errors, extraContent)
{
var contents = [errors];
if (extraContent)
{
contents = contents.concat(extraContent);
}
return DOMBuilder.createElement("tr", {}, [
DOMBuilder.createElement("td", {colSpan: 2}, contents)
]);
}
return this._htmlOutput(normalRow, errorRow, false, doNotCoerce);
};
/**
* Returns this form rendered as HTML <li>s - excluding the
* <ul></ul>.
*
* @param {Boolean} [doNotCoerce] if <code>true</code>, the resulting rows will
* not be coerced to a String if we're operating
* in HTML mode - defaults to <code>false</code>.
*/
Form.prototype.asUL = function(doNotCoerce)
{
var normalRow = function(label, field, helpText, errors, extraContent)
{
var contents = [];
if (errors)
{
contents.push(errors);
}
if (label)
{
contents.push(label);
}
contents.push(" ");
contents.push(field);
if (helpText)
{
contents.push(" ");
contents.push(helpText);
}
if (extraContent)
{
contents = contents.concat(extraContent);
}
return DOMBuilder.createElement("li", {}, contents);
};
var errorRow = function(errors, extraContent)
{
var contents = [errors];
if (extraContent)
{
contents = contents.concat(extraContent);
}
return DOMBuilder.createElement("li", {}, contents);
}
return this._htmlOutput(normalRow, errorRow, false, doNotCoerce);
};
/**
* Returns this form rendered as HTML <p>s.
*
* @param {Boolean} [doNotCoerce] if <code>true</code>, the resulting rows will
* not be coerced to a String if we're operating
* in HTML mode - defaults to <code>false</code>.
*/
Form.prototype.asP = function(doNotCoerce)
{
var normalRow = function(label, field, helpText, errors, extraContent)
{
var contents = [];
if (label)
{
contents.push(label);
}
contents.push(" ");
contents.push(field);
if (helpText)
{
contents.push(" ");
contents.push(helpText);
}
if (extraContent)
{
contents = contents.concat(extraContent);
}
return DOMBuilder.createElement("p", {}, contents);
};
var errorRow = function(errors, extraContent)
{
if (extraContent)
{
// When provided extraContent is usually hidden fields, so we need
// to give it a block scope wrapper in this case for HTML validity.
return DOMBuilder.createElement("div", {}, [errors].concat(extraContent));
}
// Otherwise, just display errors as they are
return errors;
}
return this._htmlOutput(normalRow, errorRow, true, doNotCoerce);
};
/**
* Returns errors that aren't associated with a particular field.
*
* @return errors that aren't associated with a particular field - i.e., errors
* generated by <code>clean()</code>. Will be empty if there are none.
* @type ErrorList
*/
Form.prototype.nonFieldErrors = function()
{
var errors = this.errors;
if (typeof errors[Form.NON_FIELD_ERRORS] != "undefined")
{
return errors[Form.NON_FIELD_ERRORS];
}
return new this.errorConstructor();
};
/**
* Returns the raw value for a particular field name. This is just a convenient
* wrapper around widget.valueFromData.
*/
Form.prototype._rawValue = function(fieldname)
{
var field = this.fields[fieldname];
var prefix = this.addPrefix(fieldname);
return field.widget.valueFromData(this.data, this.files, prefix);
};
/**
* Cleans all of <code>data</code> and populates <code>_errors</code> and
* <code>cleanedData</code>.
*/
Form.prototype.fullClean = function()
{
this._errors = new util.ErrorObject();
if (!this.isBound)
{
// Stop further processing
return;
}
this.cleanedData = {};
// If the form is permitted to be empty, and none of the form data has
// changed from the initial data, short circuit any validation.
if (this.emptyPermitted && !this.hasChanged())
{
return;
}
this._cleanFields();
this._cleanForm();
if (this._errors.isPopulated())
{
delete this.cleanedData;
}
};
Form.prototype._cleanFields = function()
{
for (var name in this.fields)
{
if (!this.fields.hasOwnProperty(name))
{
continue;
}
var field = this.fields[name];
// valueFromData() gets the data from the data objects.
// Each widget type knows how to retrieve its own data, because some
// widgets split data over several HTML fields.
var value = field.widget.valueFromData(this.data, this.files,
this.addPrefix(name));
try
{
if (field instanceof FileField)
{
var initial;
if (typeof this.initial[name] != "undefined")
{
initial = this.initial[name];
}
else
{
initial = field.initial;
}
value = field.clean(value, initial);
}
else
{
value = field.clean(value);
}
this.cleanedData[name] = value;
// Try clean_name
var customClean = "clean_" + name;
if (typeof this[customClean] == "function")
{
this.cleanedData[name] = this[customClean]();
continue;
}
// Try cleanName
customClean =
"clean" + name.charAt(0).toUpperCase() + name.substr(1);
if (typeof this[customClean] == "function")
{
this.cleanedData[name] = this[customClean]();
}
}
catch (e)
{
if (e instanceof ValidationError)
{
this._errors[name] = e.messages;
if (typeof this.cleanedData[name] != "undefined")
{
delete this.cleanedData[name];
}
}
else
{
throw e;
}
}
}
};
Form.prototype._cleanForm = function()
{
try
{
this.cleanedData = this.clean();
}
catch (e)
{
if (e instanceof ValidationError)
{
this._errors[Form.NON_FIELD_ERRORS] = e.messages;
}
else
{
throw e;
}
}
};
/**
* Hook for doing any extra form-wide cleaning after each Field's
* <code>clean()</code> has been called. Any {@link ValidationError} raised by
* this method will not be associated with a particular field; it will have a
* special-case association with the field named <code>"__all__"</code>.
*
* @return validated, cleaned data.
* @type Object
*/
Form.prototype.clean = function()
{
return this.cleanedData;
};
/**
* Determines if data differs from initial.
*
* @type Boolean
*/
Form.prototype.hasChanged = function()
{
return (this.changedData.length != 0);
};
/**
* Determines if the form needs to be multipart-encrypted, in other words, if it
* has a {@link FileInput}.
*
* @return <code>true</code> if the form needs to be multipart-encrypted,
* <code>false</code> otherwise.
* @type Boolean
*/
Form.prototype.isMultipart = function()
{
for (var name in this.fields)
{
if (this.fields.hasOwnProperty(name))
{
if (this.fields[name].widget.needsMultipartForm)
{
return true;
}
}
}
return false;
};
/**
* Returns a list of all the {@link BoundField} objects that correspond to
* hidden fields. Useful for manual form layout.
*
* @type Array
*/
Form.prototype.hiddenFields = function()
{
return this.boundFields(function(field)
{
return field.widget.isHidden;
});
};
/**
* Returns a list of {@link BoundField} objects that do not correspond to