~ubuntu-branches/ubuntu/jaunty/python-django/jaunty-backports

« back to all changes in this revision

Viewing changes to tests/regressiontests/forms/fields.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2008-11-15 19:15:33 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20081115191533-xbt1ut2xf4fvwtvc
Tags: 1.0.1-0ubuntu1
* New upstream release:
  - Bug fixes.

* The tests/ sub-directory appaers to have been dropped upstream, so pull
  our patch to workaround the tests and modify the rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
tests = r"""
3
 
>>> from django.forms import *
4
 
>>> from django.forms.widgets import RadioFieldRenderer
5
 
>>> from django.core.files.uploadedfile import SimpleUploadedFile
6
 
>>> import datetime
7
 
>>> import time
8
 
>>> import re
9
 
>>> try:
10
 
...     from decimal import Decimal
11
 
... except ImportError:
12
 
...     from django.utils._decimal import Decimal
13
 
 
14
 
 
15
 
##########
16
 
# Fields #
17
 
##########
18
 
 
19
 
Each Field class does some sort of validation. Each Field has a clean() method,
20
 
which either raises django.forms.ValidationError or returns the "clean"
21
 
data -- usually a Unicode object, but, in some rare cases, a list.
22
 
 
23
 
Each Field's __init__() takes at least these parameters:
24
 
    required -- Boolean that specifies whether the field is required.
25
 
                True by default.
26
 
    widget -- A Widget class, or instance of a Widget class, that should be
27
 
              used for this Field when displaying it. Each Field has a default
28
 
              Widget that it'll use if you don't specify this. In most cases,
29
 
              the default widget is TextInput.
30
 
    label -- A verbose name for this field, for use in displaying this field in
31
 
             a form. By default, Django will use a "pretty" version of the form
32
 
             field name, if the Field is part of a Form.
33
 
    initial -- A value to use in this Field's initial display. This value is
34
 
               *not* used as a fallback if data isn't given.
35
 
 
36
 
Other than that, the Field subclasses have class-specific options for
37
 
__init__(). For example, CharField has a max_length option.
38
 
 
39
 
# CharField ###################################################################
40
 
 
41
 
>>> f = CharField()
42
 
>>> f.clean(1)
43
 
u'1'
44
 
>>> f.clean('hello')
45
 
u'hello'
46
 
>>> f.clean(None)
47
 
Traceback (most recent call last):
48
 
...
49
 
ValidationError: [u'This field is required.']
50
 
>>> f.clean('')
51
 
Traceback (most recent call last):
52
 
...
53
 
ValidationError: [u'This field is required.']
54
 
>>> f.clean([1, 2, 3])
55
 
u'[1, 2, 3]'
56
 
 
57
 
>>> f = CharField(required=False)
58
 
>>> f.clean(1)
59
 
u'1'
60
 
>>> f.clean('hello')
61
 
u'hello'
62
 
>>> f.clean(None)
63
 
u''
64
 
>>> f.clean('')
65
 
u''
66
 
>>> f.clean([1, 2, 3])
67
 
u'[1, 2, 3]'
68
 
 
69
 
CharField accepts an optional max_length parameter:
70
 
>>> f = CharField(max_length=10, required=False)
71
 
>>> f.clean('12345')
72
 
u'12345'
73
 
>>> f.clean('1234567890')
74
 
u'1234567890'
75
 
>>> f.clean('1234567890a')
76
 
Traceback (most recent call last):
77
 
...
78
 
ValidationError: [u'Ensure this value has at most 10 characters (it has 11).']
79
 
 
80
 
CharField accepts an optional min_length parameter:
81
 
>>> f = CharField(min_length=10, required=False)
82
 
>>> f.clean('')
83
 
u''
84
 
>>> f.clean('12345')
85
 
Traceback (most recent call last):
86
 
...
87
 
ValidationError: [u'Ensure this value has at least 10 characters (it has 5).']
88
 
>>> f.clean('1234567890')
89
 
u'1234567890'
90
 
>>> f.clean('1234567890a')
91
 
u'1234567890a'
92
 
 
93
 
>>> f = CharField(min_length=10, required=True)
94
 
>>> f.clean('')
95
 
Traceback (most recent call last):
96
 
...
97
 
ValidationError: [u'This field is required.']
98
 
>>> f.clean('12345')
99
 
Traceback (most recent call last):
100
 
...
101
 
ValidationError: [u'Ensure this value has at least 10 characters (it has 5).']
102
 
>>> f.clean('1234567890')
103
 
u'1234567890'
104
 
>>> f.clean('1234567890a')
105
 
u'1234567890a'
106
 
 
107
 
# IntegerField ################################################################
108
 
 
109
 
>>> f = IntegerField()
110
 
>>> f.clean('')
111
 
Traceback (most recent call last):
112
 
...
113
 
ValidationError: [u'This field is required.']
114
 
>>> f.clean(None)
115
 
Traceback (most recent call last):
116
 
...
117
 
ValidationError: [u'This field is required.']
118
 
>>> f.clean('1')
119
 
1
120
 
>>> isinstance(f.clean('1'), int)
121
 
True
122
 
>>> f.clean('23')
123
 
23
124
 
>>> f.clean('a')
125
 
Traceback (most recent call last):
126
 
...
127
 
ValidationError: [u'Enter a whole number.']
128
 
>>> f.clean(42)
129
 
42
130
 
>>> f.clean(3.14)
131
 
Traceback (most recent call last):
132
 
...
133
 
ValidationError: [u'Enter a whole number.']
134
 
>>> f.clean('1 ')
135
 
1
136
 
>>> f.clean(' 1')
137
 
1
138
 
>>> f.clean(' 1 ')
139
 
1
140
 
>>> f.clean('1a')
141
 
Traceback (most recent call last):
142
 
...
143
 
ValidationError: [u'Enter a whole number.']
144
 
 
145
 
>>> f = IntegerField(required=False)
146
 
>>> f.clean('')
147
 
>>> repr(f.clean(''))
148
 
'None'
149
 
>>> f.clean(None)
150
 
>>> repr(f.clean(None))
151
 
'None'
152
 
>>> f.clean('1')
153
 
1
154
 
>>> isinstance(f.clean('1'), int)
155
 
True
156
 
>>> f.clean('23')
157
 
23
158
 
>>> f.clean('a')
159
 
Traceback (most recent call last):
160
 
...
161
 
ValidationError: [u'Enter a whole number.']
162
 
>>> f.clean('1 ')
163
 
1
164
 
>>> f.clean(' 1')
165
 
1
166
 
>>> f.clean(' 1 ')
167
 
1
168
 
>>> f.clean('1a')
169
 
Traceback (most recent call last):
170
 
...
171
 
ValidationError: [u'Enter a whole number.']
172
 
 
173
 
IntegerField accepts an optional max_value parameter:
174
 
>>> f = IntegerField(max_value=10)
175
 
>>> f.clean(None)
176
 
Traceback (most recent call last):
177
 
...
178
 
ValidationError: [u'This field is required.']
179
 
>>> f.clean(1)
180
 
1
181
 
>>> f.clean(10)
182
 
10
183
 
>>> f.clean(11)
184
 
Traceback (most recent call last):
185
 
...
186
 
ValidationError: [u'Ensure this value is less than or equal to 10.']
187
 
>>> f.clean('10')
188
 
10
189
 
>>> f.clean('11')
190
 
Traceback (most recent call last):
191
 
...
192
 
ValidationError: [u'Ensure this value is less than or equal to 10.']
193
 
 
194
 
IntegerField accepts an optional min_value parameter:
195
 
>>> f = IntegerField(min_value=10)
196
 
>>> f.clean(None)
197
 
Traceback (most recent call last):
198
 
...
199
 
ValidationError: [u'This field is required.']
200
 
>>> f.clean(1)
201
 
Traceback (most recent call last):
202
 
...
203
 
ValidationError: [u'Ensure this value is greater than or equal to 10.']
204
 
>>> f.clean(10)
205
 
10
206
 
>>> f.clean(11)
207
 
11
208
 
>>> f.clean('10')
209
 
10
210
 
>>> f.clean('11')
211
 
11
212
 
 
213
 
min_value and max_value can be used together:
214
 
>>> f = IntegerField(min_value=10, max_value=20)
215
 
>>> f.clean(None)
216
 
Traceback (most recent call last):
217
 
...
218
 
ValidationError: [u'This field is required.']
219
 
>>> f.clean(1)
220
 
Traceback (most recent call last):
221
 
...
222
 
ValidationError: [u'Ensure this value is greater than or equal to 10.']
223
 
>>> f.clean(10)
224
 
10
225
 
>>> f.clean(11)
226
 
11
227
 
>>> f.clean('10')
228
 
10
229
 
>>> f.clean('11')
230
 
11
231
 
>>> f.clean(20)
232
 
20
233
 
>>> f.clean(21)
234
 
Traceback (most recent call last):
235
 
...
236
 
ValidationError: [u'Ensure this value is less than or equal to 20.']
237
 
 
238
 
# FloatField ##################################################################
239
 
 
240
 
>>> f = FloatField()
241
 
>>> f.clean('')
242
 
Traceback (most recent call last):
243
 
...
244
 
ValidationError: [u'This field is required.']
245
 
>>> f.clean(None)
246
 
Traceback (most recent call last):
247
 
...
248
 
ValidationError: [u'This field is required.']
249
 
>>> f.clean('1')
250
 
1.0
251
 
>>> isinstance(f.clean('1'), float)
252
 
True
253
 
>>> f.clean('23')
254
 
23.0
255
 
>>> f.clean('3.14')
256
 
3.1400000000000001
257
 
>>> f.clean(3.14)
258
 
3.1400000000000001
259
 
>>> f.clean(42)
260
 
42.0
261
 
>>> f.clean('a')
262
 
Traceback (most recent call last):
263
 
...
264
 
ValidationError: [u'Enter a number.']
265
 
>>> f.clean('1.0 ')
266
 
1.0
267
 
>>> f.clean(' 1.0')
268
 
1.0
269
 
>>> f.clean(' 1.0 ')
270
 
1.0
271
 
>>> f.clean('1.0a')
272
 
Traceback (most recent call last):
273
 
...
274
 
ValidationError: [u'Enter a number.']
275
 
 
276
 
>>> f = FloatField(required=False)
277
 
>>> f.clean('')
278
 
 
279
 
>>> f.clean(None)
280
 
 
281
 
>>> f.clean('1')
282
 
1.0
283
 
 
284
 
FloatField accepts min_value and max_value just like IntegerField:
285
 
>>> f = FloatField(max_value=1.5, min_value=0.5)
286
 
 
287
 
>>> f.clean('1.6')
288
 
Traceback (most recent call last):
289
 
...
290
 
ValidationError: [u'Ensure this value is less than or equal to 1.5.']
291
 
>>> f.clean('0.4')
292
 
Traceback (most recent call last):
293
 
...
294
 
ValidationError: [u'Ensure this value is greater than or equal to 0.5.']
295
 
>>> f.clean('1.5')
296
 
1.5
297
 
>>> f.clean('0.5')
298
 
0.5
299
 
 
300
 
# DecimalField ################################################################
301
 
 
302
 
>>> f = DecimalField(max_digits=4, decimal_places=2)
303
 
>>> f.clean('')
304
 
Traceback (most recent call last):
305
 
...
306
 
ValidationError: [u'This field is required.']
307
 
>>> f.clean(None)
308
 
Traceback (most recent call last):
309
 
...
310
 
ValidationError: [u'This field is required.']
311
 
>>> f.clean('1') == Decimal("1")
312
 
True
313
 
>>> isinstance(f.clean('1'), Decimal)
314
 
True
315
 
>>> f.clean('23') == Decimal("23")
316
 
True
317
 
>>> f.clean('3.14') == Decimal("3.14")
318
 
True
319
 
>>> f.clean(3.14) == Decimal("3.14")
320
 
True
321
 
>>> f.clean(Decimal('3.14')) == Decimal("3.14")
322
 
True
323
 
>>> f.clean('a')
324
 
Traceback (most recent call last):
325
 
...
326
 
ValidationError: [u'Enter a number.']
327
 
>>> f.clean(u'łąść')
328
 
Traceback (most recent call last):
329
 
...
330
 
ValidationError: [u'Enter a number.']
331
 
>>> f.clean('1.0 ') == Decimal("1.0")
332
 
True
333
 
>>> f.clean(' 1.0') == Decimal("1.0")
334
 
True
335
 
>>> f.clean(' 1.0 ') == Decimal("1.0")
336
 
True
337
 
>>> f.clean('1.0a')
338
 
Traceback (most recent call last):
339
 
...
340
 
ValidationError: [u'Enter a number.']
341
 
>>> f.clean('123.45')
342
 
Traceback (most recent call last):
343
 
...
344
 
ValidationError: [u'Ensure that there are no more than 4 digits in total.']
345
 
>>> f.clean('1.234')
346
 
Traceback (most recent call last):
347
 
...
348
 
ValidationError: [u'Ensure that there are no more than 2 decimal places.']
349
 
>>> f.clean('123.4')
350
 
Traceback (most recent call last):
351
 
...
352
 
ValidationError: [u'Ensure that there are no more than 2 digits before the decimal point.']
353
 
>>> f.clean('-12.34') == Decimal("-12.34")
354
 
True
355
 
>>> f.clean('-123.45')
356
 
Traceback (most recent call last):
357
 
...
358
 
ValidationError: [u'Ensure that there are no more than 4 digits in total.']
359
 
>>> f.clean('-.12') == Decimal("-0.12")
360
 
True
361
 
>>> f.clean('-00.12') == Decimal("-0.12")
362
 
True
363
 
>>> f.clean('-000.12') == Decimal("-0.12")
364
 
True
365
 
>>> f.clean('-000.123')
366
 
Traceback (most recent call last):
367
 
...
368
 
ValidationError: [u'Ensure that there are no more than 2 decimal places.']
369
 
>>> f.clean('-000.1234')
370
 
Traceback (most recent call last):
371
 
...
372
 
ValidationError: [u'Ensure that there are no more than 4 digits in total.']
373
 
>>> f.clean('--0.12')
374
 
Traceback (most recent call last):
375
 
...
376
 
ValidationError: [u'Enter a number.']
377
 
 
378
 
>>> f = DecimalField(max_digits=4, decimal_places=2, required=False)
379
 
>>> f.clean('')
380
 
 
381
 
>>> f.clean(None)
382
 
 
383
 
>>> f.clean('1') == Decimal("1")
384
 
True
385
 
 
386
 
DecimalField accepts min_value and max_value just like IntegerField:
387
 
>>> f = DecimalField(max_digits=4, decimal_places=2, max_value=Decimal('1.5'), min_value=Decimal('0.5'))
388
 
 
389
 
>>> f.clean('1.6')
390
 
Traceback (most recent call last):
391
 
...
392
 
ValidationError: [u'Ensure this value is less than or equal to 1.5.']
393
 
>>> f.clean('0.4')
394
 
Traceback (most recent call last):
395
 
...
396
 
ValidationError: [u'Ensure this value is greater than or equal to 0.5.']
397
 
>>> f.clean('1.5') == Decimal("1.5")
398
 
True
399
 
>>> f.clean('0.5') == Decimal("0.5")
400
 
True
401
 
>>> f.clean('.5') == Decimal("0.5")
402
 
True
403
 
>>> f.clean('00.50') == Decimal("0.50")
404
 
True
405
 
 
406
 
 
407
 
>>> f = DecimalField(decimal_places=2)
408
 
>>> f.clean('0.00000001')
409
 
Traceback (most recent call last):
410
 
...
411
 
ValidationError: [u'Ensure that there are no more than 2 decimal places.']
412
 
 
413
 
 
414
 
>>> f = DecimalField(max_digits=3)
415
 
 
416
 
# Leading whole zeros "collapse" to one digit.
417
 
>>> f.clean('0000000.10') == Decimal("0.1")
418
 
True
419
 
>>> f.clean('0000000.100')
420
 
Traceback (most recent call last):
421
 
...
422
 
ValidationError: [u'Ensure that there are no more than 3 digits in total.']
423
 
 
424
 
# Only leading whole zeros "collapse" to one digit.
425
 
>>> f.clean('000000.02') == Decimal('0.02')
426
 
True
427
 
>>> f.clean('000000.002')
428
 
Traceback (most recent call last):
429
 
...
430
 
ValidationError: [u'Ensure that there are no more than 3 digits in total.']
431
 
 
432
 
 
433
 
# DateField ###################################################################
434
 
 
435
 
>>> import datetime
436
 
>>> f = DateField()
437
 
>>> f.clean(datetime.date(2006, 10, 25))
438
 
datetime.date(2006, 10, 25)
439
 
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
440
 
datetime.date(2006, 10, 25)
441
 
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
442
 
datetime.date(2006, 10, 25)
443
 
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
444
 
datetime.date(2006, 10, 25)
445
 
>>> f.clean('2006-10-25')
446
 
datetime.date(2006, 10, 25)
447
 
>>> f.clean('10/25/2006')
448
 
datetime.date(2006, 10, 25)
449
 
>>> f.clean('10/25/06')
450
 
datetime.date(2006, 10, 25)
451
 
>>> f.clean('Oct 25 2006')
452
 
datetime.date(2006, 10, 25)
453
 
>>> f.clean('October 25 2006')
454
 
datetime.date(2006, 10, 25)
455
 
>>> f.clean('October 25, 2006')
456
 
datetime.date(2006, 10, 25)
457
 
>>> f.clean('25 October 2006')
458
 
datetime.date(2006, 10, 25)
459
 
>>> f.clean('25 October, 2006')
460
 
datetime.date(2006, 10, 25)
461
 
>>> f.clean('2006-4-31')
462
 
Traceback (most recent call last):
463
 
...
464
 
ValidationError: [u'Enter a valid date.']
465
 
>>> f.clean('200a-10-25')
466
 
Traceback (most recent call last):
467
 
...
468
 
ValidationError: [u'Enter a valid date.']
469
 
>>> f.clean('25/10/06')
470
 
Traceback (most recent call last):
471
 
...
472
 
ValidationError: [u'Enter a valid date.']
473
 
>>> f.clean(None)
474
 
Traceback (most recent call last):
475
 
...
476
 
ValidationError: [u'This field is required.']
477
 
 
478
 
>>> f = DateField(required=False)
479
 
>>> f.clean(None)
480
 
>>> repr(f.clean(None))
481
 
'None'
482
 
>>> f.clean('')
483
 
>>> repr(f.clean(''))
484
 
'None'
485
 
 
486
 
DateField accepts an optional input_formats parameter:
487
 
>>> f = DateField(input_formats=['%Y %m %d'])
488
 
>>> f.clean(datetime.date(2006, 10, 25))
489
 
datetime.date(2006, 10, 25)
490
 
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
491
 
datetime.date(2006, 10, 25)
492
 
>>> f.clean('2006 10 25')
493
 
datetime.date(2006, 10, 25)
494
 
 
495
 
The input_formats parameter overrides all default input formats,
496
 
so the default formats won't work unless you specify them:
497
 
>>> f.clean('2006-10-25')
498
 
Traceback (most recent call last):
499
 
...
500
 
ValidationError: [u'Enter a valid date.']
501
 
>>> f.clean('10/25/2006')
502
 
Traceback (most recent call last):
503
 
...
504
 
ValidationError: [u'Enter a valid date.']
505
 
>>> f.clean('10/25/06')
506
 
Traceback (most recent call last):
507
 
...
508
 
ValidationError: [u'Enter a valid date.']
509
 
 
510
 
# TimeField ###################################################################
511
 
 
512
 
>>> import datetime
513
 
>>> f = TimeField()
514
 
>>> f.clean(datetime.time(14, 25))
515
 
datetime.time(14, 25)
516
 
>>> f.clean(datetime.time(14, 25, 59))
517
 
datetime.time(14, 25, 59)
518
 
>>> f.clean('14:25')
519
 
datetime.time(14, 25)
520
 
>>> f.clean('14:25:59')
521
 
datetime.time(14, 25, 59)
522
 
>>> f.clean('hello')
523
 
Traceback (most recent call last):
524
 
...
525
 
ValidationError: [u'Enter a valid time.']
526
 
>>> f.clean('1:24 p.m.')
527
 
Traceback (most recent call last):
528
 
...
529
 
ValidationError: [u'Enter a valid time.']
530
 
 
531
 
TimeField accepts an optional input_formats parameter:
532
 
>>> f = TimeField(input_formats=['%I:%M %p'])
533
 
>>> f.clean(datetime.time(14, 25))
534
 
datetime.time(14, 25)
535
 
>>> f.clean(datetime.time(14, 25, 59))
536
 
datetime.time(14, 25, 59)
537
 
>>> f.clean('4:25 AM')
538
 
datetime.time(4, 25)
539
 
>>> f.clean('4:25 PM')
540
 
datetime.time(16, 25)
541
 
 
542
 
The input_formats parameter overrides all default input formats,
543
 
so the default formats won't work unless you specify them:
544
 
>>> f.clean('14:30:45')
545
 
Traceback (most recent call last):
546
 
...
547
 
ValidationError: [u'Enter a valid time.']
548
 
 
549
 
# DateTimeField ###############################################################
550
 
 
551
 
>>> import datetime
552
 
>>> f = DateTimeField()
553
 
>>> f.clean(datetime.date(2006, 10, 25))
554
 
datetime.datetime(2006, 10, 25, 0, 0)
555
 
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
556
 
datetime.datetime(2006, 10, 25, 14, 30)
557
 
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
558
 
datetime.datetime(2006, 10, 25, 14, 30, 59)
559
 
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
560
 
datetime.datetime(2006, 10, 25, 14, 30, 59, 200)
561
 
>>> f.clean('2006-10-25 14:30:45')
562
 
datetime.datetime(2006, 10, 25, 14, 30, 45)
563
 
>>> f.clean('2006-10-25 14:30:00')
564
 
datetime.datetime(2006, 10, 25, 14, 30)
565
 
>>> f.clean('2006-10-25 14:30')
566
 
datetime.datetime(2006, 10, 25, 14, 30)
567
 
>>> f.clean('2006-10-25')
568
 
datetime.datetime(2006, 10, 25, 0, 0)
569
 
>>> f.clean('10/25/2006 14:30:45')
570
 
datetime.datetime(2006, 10, 25, 14, 30, 45)
571
 
>>> f.clean('10/25/2006 14:30:00')
572
 
datetime.datetime(2006, 10, 25, 14, 30)
573
 
>>> f.clean('10/25/2006 14:30')
574
 
datetime.datetime(2006, 10, 25, 14, 30)
575
 
>>> f.clean('10/25/2006')
576
 
datetime.datetime(2006, 10, 25, 0, 0)
577
 
>>> f.clean('10/25/06 14:30:45')
578
 
datetime.datetime(2006, 10, 25, 14, 30, 45)
579
 
>>> f.clean('10/25/06 14:30:00')
580
 
datetime.datetime(2006, 10, 25, 14, 30)
581
 
>>> f.clean('10/25/06 14:30')
582
 
datetime.datetime(2006, 10, 25, 14, 30)
583
 
>>> f.clean('10/25/06')
584
 
datetime.datetime(2006, 10, 25, 0, 0)
585
 
>>> f.clean('hello')
586
 
Traceback (most recent call last):
587
 
...
588
 
ValidationError: [u'Enter a valid date/time.']
589
 
>>> f.clean('2006-10-25 4:30 p.m.')
590
 
Traceback (most recent call last):
591
 
...
592
 
ValidationError: [u'Enter a valid date/time.']
593
 
 
594
 
DateField accepts an optional input_formats parameter:
595
 
>>> f = DateTimeField(input_formats=['%Y %m %d %I:%M %p'])
596
 
>>> f.clean(datetime.date(2006, 10, 25))
597
 
datetime.datetime(2006, 10, 25, 0, 0)
598
 
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30))
599
 
datetime.datetime(2006, 10, 25, 14, 30)
600
 
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))
601
 
datetime.datetime(2006, 10, 25, 14, 30, 59)
602
 
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))
603
 
datetime.datetime(2006, 10, 25, 14, 30, 59, 200)
604
 
>>> f.clean('2006 10 25 2:30 PM')
605
 
datetime.datetime(2006, 10, 25, 14, 30)
606
 
 
607
 
The input_formats parameter overrides all default input formats,
608
 
so the default formats won't work unless you specify them:
609
 
>>> f.clean('2006-10-25 14:30:45')
610
 
Traceback (most recent call last):
611
 
...
612
 
ValidationError: [u'Enter a valid date/time.']
613
 
 
614
 
>>> f = DateTimeField(required=False)
615
 
>>> f.clean(None)
616
 
>>> repr(f.clean(None))
617
 
'None'
618
 
>>> f.clean('')
619
 
>>> repr(f.clean(''))
620
 
'None'
621
 
 
622
 
# RegexField ##################################################################
623
 
 
624
 
>>> f = RegexField('^\d[A-F]\d$')
625
 
>>> f.clean('2A2')
626
 
u'2A2'
627
 
>>> f.clean('3F3')
628
 
u'3F3'
629
 
>>> f.clean('3G3')
630
 
Traceback (most recent call last):
631
 
...
632
 
ValidationError: [u'Enter a valid value.']
633
 
>>> f.clean(' 2A2')
634
 
Traceback (most recent call last):
635
 
...
636
 
ValidationError: [u'Enter a valid value.']
637
 
>>> f.clean('2A2 ')
638
 
Traceback (most recent call last):
639
 
...
640
 
ValidationError: [u'Enter a valid value.']
641
 
>>> f.clean('')
642
 
Traceback (most recent call last):
643
 
...
644
 
ValidationError: [u'This field is required.']
645
 
 
646
 
>>> f = RegexField('^\d[A-F]\d$', required=False)
647
 
>>> f.clean('2A2')
648
 
u'2A2'
649
 
>>> f.clean('3F3')
650
 
u'3F3'
651
 
>>> f.clean('3G3')
652
 
Traceback (most recent call last):
653
 
...
654
 
ValidationError: [u'Enter a valid value.']
655
 
>>> f.clean('')
656
 
u''
657
 
 
658
 
Alternatively, RegexField can take a compiled regular expression:
659
 
>>> f = RegexField(re.compile('^\d[A-F]\d$'))
660
 
>>> f.clean('2A2')
661
 
u'2A2'
662
 
>>> f.clean('3F3')
663
 
u'3F3'
664
 
>>> f.clean('3G3')
665
 
Traceback (most recent call last):
666
 
...
667
 
ValidationError: [u'Enter a valid value.']
668
 
>>> f.clean(' 2A2')
669
 
Traceback (most recent call last):
670
 
...
671
 
ValidationError: [u'Enter a valid value.']
672
 
>>> f.clean('2A2 ')
673
 
Traceback (most recent call last):
674
 
...
675
 
ValidationError: [u'Enter a valid value.']
676
 
 
677
 
RegexField takes an optional error_message argument:
678
 
>>> f = RegexField('^\d\d\d\d$', error_message='Enter a four-digit number.')
679
 
>>> f.clean('1234')
680
 
u'1234'
681
 
>>> f.clean('123')
682
 
Traceback (most recent call last):
683
 
...
684
 
ValidationError: [u'Enter a four-digit number.']
685
 
>>> f.clean('abcd')
686
 
Traceback (most recent call last):
687
 
...
688
 
ValidationError: [u'Enter a four-digit number.']
689
 
 
690
 
RegexField also access min_length and max_length parameters, for convenience.
691
 
>>> f = RegexField('^\d+$', min_length=5, max_length=10)
692
 
>>> f.clean('123')
693
 
Traceback (most recent call last):
694
 
...
695
 
ValidationError: [u'Ensure this value has at least 5 characters (it has 3).']
696
 
>>> f.clean('abc')
697
 
Traceback (most recent call last):
698
 
...
699
 
ValidationError: [u'Ensure this value has at least 5 characters (it has 3).']
700
 
>>> f.clean('12345')
701
 
u'12345'
702
 
>>> f.clean('1234567890')
703
 
u'1234567890'
704
 
>>> f.clean('12345678901')
705
 
Traceback (most recent call last):
706
 
...
707
 
ValidationError: [u'Ensure this value has at most 10 characters (it has 11).']
708
 
>>> f.clean('12345a')
709
 
Traceback (most recent call last):
710
 
...
711
 
ValidationError: [u'Enter a valid value.']
712
 
 
713
 
# EmailField ##################################################################
714
 
 
715
 
>>> f = EmailField()
716
 
>>> f.clean('')
717
 
Traceback (most recent call last):
718
 
...
719
 
ValidationError: [u'This field is required.']
720
 
>>> f.clean(None)
721
 
Traceback (most recent call last):
722
 
...
723
 
ValidationError: [u'This field is required.']
724
 
>>> f.clean('person@example.com')
725
 
u'person@example.com'
726
 
>>> f.clean('foo')
727
 
Traceback (most recent call last):
728
 
...
729
 
ValidationError: [u'Enter a valid e-mail address.']
730
 
>>> f.clean('foo@')
731
 
Traceback (most recent call last):
732
 
...
733
 
ValidationError: [u'Enter a valid e-mail address.']
734
 
>>> f.clean('foo@bar')
735
 
Traceback (most recent call last):
736
 
...
737
 
ValidationError: [u'Enter a valid e-mail address.']
738
 
 
739
 
>>> f = EmailField(required=False)
740
 
>>> f.clean('')
741
 
u''
742
 
>>> f.clean(None)
743
 
u''
744
 
>>> f.clean('person@example.com')
745
 
u'person@example.com'
746
 
>>> f.clean('foo')
747
 
Traceback (most recent call last):
748
 
...
749
 
ValidationError: [u'Enter a valid e-mail address.']
750
 
>>> f.clean('foo@')
751
 
Traceback (most recent call last):
752
 
...
753
 
ValidationError: [u'Enter a valid e-mail address.']
754
 
>>> f.clean('foo@bar')
755
 
Traceback (most recent call last):
756
 
...
757
 
ValidationError: [u'Enter a valid e-mail address.']
758
 
 
759
 
EmailField also access min_length and max_length parameters, for convenience.
760
 
>>> f = EmailField(min_length=10, max_length=15)
761
 
>>> f.clean('a@foo.com')
762
 
Traceback (most recent call last):
763
 
...
764
 
ValidationError: [u'Ensure this value has at least 10 characters (it has 9).']
765
 
>>> f.clean('alf@foo.com')
766
 
u'alf@foo.com'
767
 
>>> f.clean('alf123456788@foo.com')
768
 
Traceback (most recent call last):
769
 
...
770
 
ValidationError: [u'Ensure this value has at most 15 characters (it has 20).']
771
 
 
772
 
# FileField ##################################################################
773
 
 
774
 
>>> f = FileField()
775
 
>>> f.clean('')
776
 
Traceback (most recent call last):
777
 
...
778
 
ValidationError: [u'This field is required.']
779
 
 
780
 
>>> f.clean('', '')
781
 
Traceback (most recent call last):
782
 
...
783
 
ValidationError: [u'This field is required.']
784
 
 
785
 
>>> f.clean('', 'files/test1.pdf')
786
 
'files/test1.pdf'
787
 
 
788
 
>>> f.clean(None)
789
 
Traceback (most recent call last):
790
 
...
791
 
ValidationError: [u'This field is required.']
792
 
 
793
 
>>> f.clean(None, '')
794
 
Traceback (most recent call last):
795
 
...
796
 
ValidationError: [u'This field is required.']
797
 
 
798
 
>>> f.clean(None, 'files/test2.pdf')
799
 
'files/test2.pdf'
800
 
 
801
 
>>> f.clean(SimpleUploadedFile('', ''))
802
 
Traceback (most recent call last):
803
 
...
804
 
ValidationError: [u'No file was submitted. Check the encoding type on the form.']
805
 
 
806
 
>>> f.clean(SimpleUploadedFile('', ''), '')
807
 
Traceback (most recent call last):
808
 
...
809
 
ValidationError: [u'No file was submitted. Check the encoding type on the form.']
810
 
 
811
 
>>> f.clean(None, 'files/test3.pdf')
812
 
'files/test3.pdf'
813
 
 
814
 
>>> f.clean('some content that is not a file')
815
 
Traceback (most recent call last):
816
 
...
817
 
ValidationError: [u'No file was submitted. Check the encoding type on the form.']
818
 
 
819
 
>>> f.clean(SimpleUploadedFile('name', None))
820
 
Traceback (most recent call last):
821
 
...
822
 
ValidationError: [u'The submitted file is empty.']
823
 
 
824
 
>>> f.clean(SimpleUploadedFile('name', ''))
825
 
Traceback (most recent call last):
826
 
...
827
 
ValidationError: [u'The submitted file is empty.']
828
 
 
829
 
>>> type(f.clean(SimpleUploadedFile('name', 'Some File Content')))
830
 
<class 'django.core.files.uploadedfile.SimpleUploadedFile'>
831
 
 
832
 
>>> type(f.clean(SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह')))
833
 
<class 'django.core.files.uploadedfile.SimpleUploadedFile'>
834
 
 
835
 
>>> type(f.clean(SimpleUploadedFile('name', 'Some File Content'), 'files/test4.pdf'))
836
 
<class 'django.core.files.uploadedfile.SimpleUploadedFile'>
837
 
 
838
 
# URLField ##################################################################
839
 
 
840
 
>>> f = URLField()
841
 
>>> f.clean('')
842
 
Traceback (most recent call last):
843
 
...
844
 
ValidationError: [u'This field is required.']
845
 
>>> f.clean(None)
846
 
Traceback (most recent call last):
847
 
...
848
 
ValidationError: [u'This field is required.']
849
 
>>> f.clean('http://localhost')
850
 
u'http://localhost/'
851
 
>>> f.clean('http://example.com')
852
 
u'http://example.com/'
853
 
>>> f.clean('http://www.example.com')
854
 
u'http://www.example.com/'
855
 
>>> f.clean('http://www.example.com:8000/test')
856
 
u'http://www.example.com:8000/test'
857
 
>>> f.clean('http://200.8.9.10')
858
 
u'http://200.8.9.10/'
859
 
>>> f.clean('http://200.8.9.10:8000/test')
860
 
u'http://200.8.9.10:8000/test'
861
 
>>> f.clean('foo')
862
 
Traceback (most recent call last):
863
 
...
864
 
ValidationError: [u'Enter a valid URL.']
865
 
>>> f.clean('http://')
866
 
Traceback (most recent call last):
867
 
...
868
 
ValidationError: [u'Enter a valid URL.']
869
 
>>> f.clean('http://example')
870
 
Traceback (most recent call last):
871
 
...
872
 
ValidationError: [u'Enter a valid URL.']
873
 
>>> f.clean('http://example.')
874
 
Traceback (most recent call last):
875
 
...
876
 
ValidationError: [u'Enter a valid URL.']
877
 
>>> f.clean('http://.com')
878
 
Traceback (most recent call last):
879
 
...
880
 
ValidationError: [u'Enter a valid URL.']
881
 
 
882
 
>>> f = URLField(required=False)
883
 
>>> f.clean('')
884
 
u''
885
 
>>> f.clean(None)
886
 
u''
887
 
>>> f.clean('http://example.com')
888
 
u'http://example.com/'
889
 
>>> f.clean('http://www.example.com')
890
 
u'http://www.example.com/'
891
 
>>> f.clean('foo')
892
 
Traceback (most recent call last):
893
 
...
894
 
ValidationError: [u'Enter a valid URL.']
895
 
>>> f.clean('http://')
896
 
Traceback (most recent call last):
897
 
...
898
 
ValidationError: [u'Enter a valid URL.']
899
 
>>> f.clean('http://example')
900
 
Traceback (most recent call last):
901
 
...
902
 
ValidationError: [u'Enter a valid URL.']
903
 
>>> f.clean('http://example.')
904
 
Traceback (most recent call last):
905
 
...
906
 
ValidationError: [u'Enter a valid URL.']
907
 
>>> f.clean('http://.com')
908
 
Traceback (most recent call last):
909
 
...
910
 
ValidationError: [u'Enter a valid URL.']
911
 
 
912
 
URLField takes an optional verify_exists parameter, which is False by default.
913
 
This verifies that the URL is live on the Internet and doesn't return a 404 or 500:
914
 
>>> f = URLField(verify_exists=True)
915
 
>>> f.clean('http://www.google.com') # This will fail if there's no Internet connection
916
 
u'http://www.google.com/'
917
 
>>> f.clean('http://example')
918
 
Traceback (most recent call last):
919
 
...
920
 
ValidationError: [u'Enter a valid URL.']
921
 
>>> f.clean('http://www.broken.djangoproject.com') # bad domain
922
 
Traceback (most recent call last):
923
 
...
924
 
ValidationError: [u'This URL appears to be a broken link.']
925
 
>>> f.clean('http://google.com/we-love-microsoft.html') # good domain, bad page
926
 
Traceback (most recent call last):
927
 
...
928
 
ValidationError: [u'This URL appears to be a broken link.']
929
 
>>> f = URLField(verify_exists=True, required=False)
930
 
>>> f.clean('')
931
 
u''
932
 
>>> f.clean('http://www.google.com') # This will fail if there's no Internet connection
933
 
u'http://www.google.com/'
934
 
 
935
 
URLField also access min_length and max_length parameters, for convenience.
936
 
>>> f = URLField(min_length=15, max_length=20)
937
 
>>> f.clean('http://f.com')
938
 
Traceback (most recent call last):
939
 
...
940
 
ValidationError: [u'Ensure this value has at least 15 characters (it has 13).']
941
 
>>> f.clean('http://example.com')
942
 
u'http://example.com/'
943
 
>>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com')
944
 
Traceback (most recent call last):
945
 
...
946
 
ValidationError: [u'Ensure this value has at most 20 characters (it has 38).']
947
 
 
948
 
URLField should prepend 'http://' if no scheme was given
949
 
>>> f = URLField(required=False)
950
 
>>> f.clean('example.com')
951
 
u'http://example.com/'
952
 
>>> f.clean('')
953
 
u''
954
 
>>> f.clean('https://example.com')
955
 
u'https://example.com/'
956
 
 
957
 
URLField should append '/' if no path was given
958
 
>>> f = URLField()
959
 
>>> f.clean('http://example.com')
960
 
u'http://example.com/'
961
 
 
962
 
URLField shouldn't change the path if it was given
963
 
>>> f.clean('http://example.com/test')
964
 
u'http://example.com/test'
965
 
 
966
 
# BooleanField ################################################################
967
 
 
968
 
>>> f = BooleanField()
969
 
>>> f.clean('')
970
 
Traceback (most recent call last):
971
 
...
972
 
ValidationError: [u'This field is required.']
973
 
>>> f.clean(None)
974
 
Traceback (most recent call last):
975
 
...
976
 
ValidationError: [u'This field is required.']
977
 
>>> f.clean(True)
978
 
True
979
 
>>> f.clean(False)
980
 
Traceback (most recent call last):
981
 
...
982
 
ValidationError: [u'This field is required.']
983
 
>>> f.clean(1)
984
 
True
985
 
>>> f.clean(0)
986
 
Traceback (most recent call last):
987
 
...
988
 
ValidationError: [u'This field is required.']
989
 
>>> f.clean('Django rocks')
990
 
True
991
 
 
992
 
>>> f.clean('True')
993
 
True
994
 
>>> f.clean('False')
995
 
Traceback (most recent call last):
996
 
...
997
 
ValidationError: [u'This field is required.']
998
 
 
999
 
>>> f = BooleanField(required=False)
1000
 
>>> f.clean('')
1001
 
False
1002
 
>>> f.clean(None)
1003
 
False
1004
 
>>> f.clean(True)
1005
 
True
1006
 
>>> f.clean(False)
1007
 
False
1008
 
>>> f.clean(1)
1009
 
True
1010
 
>>> f.clean(0)
1011
 
False
1012
 
>>> f.clean('Django rocks')
1013
 
True
1014
 
 
1015
 
A form's BooleanField with a hidden widget will output the string 'False', so
1016
 
that should clean to the boolean value False:
1017
 
>>> f.clean('False')
1018
 
False
1019
 
 
1020
 
# ChoiceField #################################################################
1021
 
 
1022
 
>>> f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')])
1023
 
>>> f.clean('')
1024
 
Traceback (most recent call last):
1025
 
...
1026
 
ValidationError: [u'This field is required.']
1027
 
>>> f.clean(None)
1028
 
Traceback (most recent call last):
1029
 
...
1030
 
ValidationError: [u'This field is required.']
1031
 
>>> f.clean(1)
1032
 
u'1'
1033
 
>>> f.clean('1')
1034
 
u'1'
1035
 
>>> f.clean('3')
1036
 
Traceback (most recent call last):
1037
 
...
1038
 
ValidationError: [u'Select a valid choice. 3 is not one of the available choices.']
1039
 
 
1040
 
>>> f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False)
1041
 
>>> f.clean('')
1042
 
u''
1043
 
>>> f.clean(None)
1044
 
u''
1045
 
>>> f.clean(1)
1046
 
u'1'
1047
 
>>> f.clean('1')
1048
 
u'1'
1049
 
>>> f.clean('3')
1050
 
Traceback (most recent call last):
1051
 
...
1052
 
ValidationError: [u'Select a valid choice. 3 is not one of the available choices.']
1053
 
 
1054
 
>>> f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')])
1055
 
>>> f.clean('J')
1056
 
u'J'
1057
 
>>> f.clean('John')
1058
 
Traceback (most recent call last):
1059
 
...
1060
 
ValidationError: [u'Select a valid choice. John is not one of the available choices.']
1061
 
 
1062
 
>>> f = ChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')])
1063
 
>>> f.clean(1)
1064
 
u'1'
1065
 
>>> f.clean('1')
1066
 
u'1'
1067
 
>>> f.clean(3)
1068
 
u'3'
1069
 
>>> f.clean('3')
1070
 
u'3'
1071
 
>>> f.clean(5)
1072
 
u'5'
1073
 
>>> f.clean('5')
1074
 
u'5'
1075
 
>>> f.clean('6')
1076
 
Traceback (most recent call last):
1077
 
...
1078
 
ValidationError: [u'Select a valid choice. 6 is not one of the available choices.']
1079
 
 
1080
 
# TypedChoiceField ############################################################
1081
 
 
1082
 
# TypedChoiceField is just like ChoiceField, except that coerced types will 
1083
 
# be returned:
1084
 
>>> f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int)
1085
 
>>> f.clean('1')
1086
 
1
1087
 
>>> f.clean('2')
1088
 
Traceback (most recent call last):
1089
 
...
1090
 
ValidationError: [u'Select a valid choice. 2 is not one of the available choices.']
1091
 
 
1092
 
# Different coercion, same validation.
1093
 
>>> f.coerce = float
1094
 
>>> f.clean('1')
1095
 
1.0
1096
 
 
1097
 
 
1098
 
# This can also cause weirdness: be careful (bool(-1) == True, remember)
1099
 
>>> f.coerce = bool
1100
 
>>> f.clean('-1') 
1101
 
True
1102
 
 
1103
 
# Even more weirdness: if you have a valid choice but your coercion function
1104
 
# can't coerce, you'll still get a validation error. Don't do this!
1105
 
>>> f = TypedChoiceField(choices=[('A', 'A'), ('B', 'B')], coerce=int)
1106
 
>>> f.clean('B')
1107
 
Traceback (most recent call last):
1108
 
...
1109
 
ValidationError: [u'Select a valid choice. B is not one of the available choices.']
1110
 
 
1111
 
# Required fields require values
1112
 
>>> f.clean('')
1113
 
Traceback (most recent call last):
1114
 
...
1115
 
ValidationError: [u'This field is required.']
1116
 
 
1117
 
# Non-required fields aren't required
1118
 
>>> f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False)
1119
 
>>> f.clean('')
1120
 
''
1121
 
 
1122
 
# If you want cleaning an empty value to return a different type, tell the field
1123
 
>>> f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False, empty_value=None)
1124
 
>>> print f.clean('')
1125
 
None
1126
 
 
1127
 
# NullBooleanField ############################################################
1128
 
 
1129
 
>>> f = NullBooleanField()
1130
 
>>> f.clean('')
1131
 
>>> f.clean(True)
1132
 
True
1133
 
>>> f.clean(False)
1134
 
False
1135
 
>>> f.clean(None)
1136
 
>>> f.clean('1')
1137
 
>>> f.clean('2')
1138
 
>>> f.clean('3')
1139
 
>>> f.clean('hello')
1140
 
 
1141
 
# Make sure that the internal value is preserved if using HiddenInput (#7753)
1142
 
>>> class HiddenNullBooleanForm(Form):
1143
 
...     hidden_nullbool1 = NullBooleanField(widget=HiddenInput, initial=True)
1144
 
...     hidden_nullbool2 = NullBooleanField(widget=HiddenInput, initial=False)
1145
 
>>> f = HiddenNullBooleanForm()
1146
 
>>> print f
1147
 
<input type="hidden" name="hidden_nullbool1" value="True" id="id_hidden_nullbool1" /><input type="hidden" name="hidden_nullbool2" value="False" id="id_hidden_nullbool2" />
1148
 
>>> f = HiddenNullBooleanForm({ 'hidden_nullbool1': 'True', 'hidden_nullbool2': 'False' })
1149
 
>>> f.full_clean()
1150
 
>>> f.cleaned_data['hidden_nullbool1']
1151
 
True
1152
 
>>> f.cleaned_data['hidden_nullbool2']
1153
 
False
1154
 
 
1155
 
# MultipleChoiceField #########################################################
1156
 
 
1157
 
>>> f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')])
1158
 
>>> f.clean('')
1159
 
Traceback (most recent call last):
1160
 
...
1161
 
ValidationError: [u'This field is required.']
1162
 
>>> f.clean(None)
1163
 
Traceback (most recent call last):
1164
 
...
1165
 
ValidationError: [u'This field is required.']
1166
 
>>> f.clean([1])
1167
 
[u'1']
1168
 
>>> f.clean(['1'])
1169
 
[u'1']
1170
 
>>> f.clean(['1', '2'])
1171
 
[u'1', u'2']
1172
 
>>> f.clean([1, '2'])
1173
 
[u'1', u'2']
1174
 
>>> f.clean((1, '2'))
1175
 
[u'1', u'2']
1176
 
>>> f.clean('hello')
1177
 
Traceback (most recent call last):
1178
 
...
1179
 
ValidationError: [u'Enter a list of values.']
1180
 
>>> f.clean([])
1181
 
Traceback (most recent call last):
1182
 
...
1183
 
ValidationError: [u'This field is required.']
1184
 
>>> f.clean(())
1185
 
Traceback (most recent call last):
1186
 
...
1187
 
ValidationError: [u'This field is required.']
1188
 
>>> f.clean(['3'])
1189
 
Traceback (most recent call last):
1190
 
...
1191
 
ValidationError: [u'Select a valid choice. 3 is not one of the available choices.']
1192
 
 
1193
 
>>> f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False)
1194
 
>>> f.clean('')
1195
 
[]
1196
 
>>> f.clean(None)
1197
 
[]
1198
 
>>> f.clean([1])
1199
 
[u'1']
1200
 
>>> f.clean(['1'])
1201
 
[u'1']
1202
 
>>> f.clean(['1', '2'])
1203
 
[u'1', u'2']
1204
 
>>> f.clean([1, '2'])
1205
 
[u'1', u'2']
1206
 
>>> f.clean((1, '2'))
1207
 
[u'1', u'2']
1208
 
>>> f.clean('hello')
1209
 
Traceback (most recent call last):
1210
 
...
1211
 
ValidationError: [u'Enter a list of values.']
1212
 
>>> f.clean([])
1213
 
[]
1214
 
>>> f.clean(())
1215
 
[]
1216
 
>>> f.clean(['3'])
1217
 
Traceback (most recent call last):
1218
 
...
1219
 
ValidationError: [u'Select a valid choice. 3 is not one of the available choices.']
1220
 
 
1221
 
>>> f = MultipleChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')])
1222
 
>>> f.clean([1])
1223
 
[u'1']
1224
 
>>> f.clean(['1'])
1225
 
[u'1']
1226
 
>>> f.clean([1, 5])
1227
 
[u'1', u'5']
1228
 
>>> f.clean([1, '5'])
1229
 
[u'1', u'5']
1230
 
>>> f.clean(['1', 5])
1231
 
[u'1', u'5']
1232
 
>>> f.clean(['1', '5'])
1233
 
[u'1', u'5']
1234
 
>>> f.clean(['6'])
1235
 
Traceback (most recent call last):
1236
 
...
1237
 
ValidationError: [u'Select a valid choice. 6 is not one of the available choices.']
1238
 
>>> f.clean(['1','6'])
1239
 
Traceback (most recent call last):
1240
 
...
1241
 
ValidationError: [u'Select a valid choice. 6 is not one of the available choices.']
1242
 
 
1243
 
 
1244
 
# ComboField ##################################################################
1245
 
 
1246
 
ComboField takes a list of fields that should be used to validate a value,
1247
 
in that order.
1248
 
>>> f = ComboField(fields=[CharField(max_length=20), EmailField()])
1249
 
>>> f.clean('test@example.com')
1250
 
u'test@example.com'
1251
 
>>> f.clean('longemailaddress@example.com')
1252
 
Traceback (most recent call last):
1253
 
...
1254
 
ValidationError: [u'Ensure this value has at most 20 characters (it has 28).']
1255
 
>>> f.clean('not an e-mail')
1256
 
Traceback (most recent call last):
1257
 
...
1258
 
ValidationError: [u'Enter a valid e-mail address.']
1259
 
>>> f.clean('')
1260
 
Traceback (most recent call last):
1261
 
...
1262
 
ValidationError: [u'This field is required.']
1263
 
>>> f.clean(None)
1264
 
Traceback (most recent call last):
1265
 
...
1266
 
ValidationError: [u'This field is required.']
1267
 
 
1268
 
>>> f = ComboField(fields=[CharField(max_length=20), EmailField()], required=False)
1269
 
>>> f.clean('test@example.com')
1270
 
u'test@example.com'
1271
 
>>> f.clean('longemailaddress@example.com')
1272
 
Traceback (most recent call last):
1273
 
...
1274
 
ValidationError: [u'Ensure this value has at most 20 characters (it has 28).']
1275
 
>>> f.clean('not an e-mail')
1276
 
Traceback (most recent call last):
1277
 
...
1278
 
ValidationError: [u'Enter a valid e-mail address.']
1279
 
>>> f.clean('')
1280
 
u''
1281
 
>>> f.clean(None)
1282
 
u''
1283
 
 
1284
 
# FilePathField ###############################################################
1285
 
 
1286
 
>>> def fix_os_paths(x):
1287
 
...     if isinstance(x, basestring):
1288
 
...         return x.replace('\\', '/')
1289
 
...     elif isinstance(x, tuple):
1290
 
...         return tuple(fix_os_paths(list(x)))
1291
 
...     elif isinstance(x, list):
1292
 
...         return [fix_os_paths(y) for y in x]
1293
 
...     else:
1294
 
...         return x
1295
 
...
1296
 
>>> import os
1297
 
>>> from django import forms
1298
 
>>> path = forms.__file__
1299
 
>>> path = os.path.dirname(path) + '/'
1300
 
>>> fix_os_paths(path)
1301
 
'.../django/forms/'
1302
 
>>> f = forms.FilePathField(path=path)
1303
 
>>> f.choices.sort()
1304
 
>>> fix_os_paths(f.choices)
1305
 
[('.../django/forms/__init__.py', '__init__.py'), ('.../django/forms/__init__.pyc', '__init__.pyc'), ('.../django/forms/fields.py', 'fields.py'), ('.../django/forms/fields.pyc', 'fields.pyc'), ('.../django/forms/forms.py', 'forms.py'), ('.../django/forms/forms.pyc', 'forms.pyc'), ('.../django/forms/models.py', 'models.py'), ('.../django/forms/models.pyc', 'models.pyc'), ('.../django/forms/util.py', 'util.py'), ('.../django/forms/util.pyc', 'util.pyc'), ('.../django/forms/widgets.py', 'widgets.py'), ('.../django/forms/widgets.pyc', 'widgets.pyc')]
1306
 
>>> f.clean('fields.py')
1307
 
Traceback (most recent call last):
1308
 
...
1309
 
ValidationError: [u'Select a valid choice. fields.py is not one of the available choices.']
1310
 
>>> fix_os_paths(f.clean(path + 'fields.py'))
1311
 
u'.../django/forms/fields.py'
1312
 
>>> f = forms.FilePathField(path=path, match='^.*?\.py$')
1313
 
>>> f.choices.sort()
1314
 
>>> fix_os_paths(f.choices)
1315
 
[('.../django/forms/__init__.py', '__init__.py'), ('.../django/forms/fields.py', 'fields.py'), ('.../django/forms/forms.py', 'forms.py'), ('.../django/forms/models.py', 'models.py'), ('.../django/forms/util.py', 'util.py'), ('.../django/forms/widgets.py', 'widgets.py')]
1316
 
>>> f = forms.FilePathField(path=path, recursive=True, match='^.*?\.py$')
1317
 
>>> f.choices.sort()
1318
 
>>> fix_os_paths(f.choices)
1319
 
[('.../django/forms/__init__.py', '__init__.py'), ('.../django/forms/extras/__init__.py', 'extras/__init__.py'), ('.../django/forms/extras/widgets.py', 'extras/widgets.py'), ('.../django/forms/fields.py', 'fields.py'), ('.../django/forms/forms.py', 'forms.py'), ('.../django/forms/models.py', 'models.py'), ('.../django/forms/util.py', 'util.py'), ('.../django/forms/widgets.py', 'widgets.py')]
1320
 
 
1321
 
# SplitDateTimeField ##########################################################
1322
 
 
1323
 
>>> f = SplitDateTimeField()
1324
 
>>> f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)])
1325
 
datetime.datetime(2006, 1, 10, 7, 30)
1326
 
>>> f.clean(None)
1327
 
Traceback (most recent call last):
1328
 
...
1329
 
ValidationError: [u'This field is required.']
1330
 
>>> f.clean('')
1331
 
Traceback (most recent call last):
1332
 
...
1333
 
ValidationError: [u'This field is required.']
1334
 
>>> f.clean('hello')
1335
 
Traceback (most recent call last):
1336
 
...
1337
 
ValidationError: [u'Enter a list of values.']
1338
 
>>> f.clean(['hello', 'there'])
1339
 
Traceback (most recent call last):
1340
 
...
1341
 
ValidationError: [u'Enter a valid date.', u'Enter a valid time.']
1342
 
>>> f.clean(['2006-01-10', 'there'])
1343
 
Traceback (most recent call last):
1344
 
...
1345
 
ValidationError: [u'Enter a valid time.']
1346
 
>>> f.clean(['hello', '07:30'])
1347
 
Traceback (most recent call last):
1348
 
...
1349
 
ValidationError: [u'Enter a valid date.']
1350
 
 
1351
 
>>> f = SplitDateTimeField(required=False)
1352
 
>>> f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)])
1353
 
datetime.datetime(2006, 1, 10, 7, 30)
1354
 
>>> f.clean(['2006-01-10', '07:30'])
1355
 
datetime.datetime(2006, 1, 10, 7, 30)
1356
 
>>> f.clean(None)
1357
 
>>> f.clean('')
1358
 
>>> f.clean([''])
1359
 
>>> f.clean(['', ''])
1360
 
>>> f.clean('hello')
1361
 
Traceback (most recent call last):
1362
 
...
1363
 
ValidationError: [u'Enter a list of values.']
1364
 
>>> f.clean(['hello', 'there'])
1365
 
Traceback (most recent call last):
1366
 
...
1367
 
ValidationError: [u'Enter a valid date.', u'Enter a valid time.']
1368
 
>>> f.clean(['2006-01-10', 'there'])
1369
 
Traceback (most recent call last):
1370
 
...
1371
 
ValidationError: [u'Enter a valid time.']
1372
 
>>> f.clean(['hello', '07:30'])
1373
 
Traceback (most recent call last):
1374
 
...
1375
 
ValidationError: [u'Enter a valid date.']
1376
 
>>> f.clean(['2006-01-10', ''])
1377
 
Traceback (most recent call last):
1378
 
...
1379
 
ValidationError: [u'Enter a valid time.']
1380
 
>>> f.clean(['2006-01-10'])
1381
 
Traceback (most recent call last):
1382
 
...
1383
 
ValidationError: [u'Enter a valid time.']
1384
 
>>> f.clean(['', '07:30'])
1385
 
Traceback (most recent call last):
1386
 
...
1387
 
ValidationError: [u'Enter a valid date.']
1388
 
"""