Code

Ticket #4018: django-make-initial-newforms-value-callable.2.diff

File django-make-initial-newforms-value-callable.2.diff, 4.7 KB (added by David Danier <goliath.mailinglist@…>, 7 years ago)

Patch including tests

Line 
1==== Patch <django-make-initial-newforms-value-callable> level 1
2Source: f4626db1-382e-0410-8594-a82d82b39f71:/make-initial-newforms-value-callable/trunk:4942 [local]
3Target: bcc190cf-cafb-0310-a4f2-bffc1f526a37:/django/trunk:5061 [mirrored]
4        (http://code.djangoproject.com/svn/django)
5Log:
6 r4875@damasonium:  ddanier | 2007-04-16 14:25:37 +0200
7  * Initial value for newforms-fields may be callable now
8 r4942@damasonium:  ddanier | 2007-04-23 10:14:39 +0200
9  * Tests for callable initial data
10
11=== django/newforms/forms.py
12==================================================================
13--- django/newforms/forms.py    (revision 5061)
14+++ django/newforms/forms.py    (patch django-make-initial-newforms-value-callable level 1)
15@@ -248,6 +248,8 @@
16             attrs['id'] = auto_id
17         if not self.form.is_bound:
18             data = self.form.initial.get(self.name, self.field.initial)
19+            if callable(data):
20+                data = data()
21         else:
22             data = self.data
23         return widget.render(self.html_name, data, attrs=attrs)
24=== tests/regressiontests/forms/tests.py
25==================================================================
26--- tests/regressiontests/forms/tests.py        (revision 5061)
27+++ tests/regressiontests/forms/tests.py        (patch django-make-initial-newforms-value-callable level 1)
28@@ -2704,6 +2704,64 @@
29 <li>Username: <input type="text" name="username" value="babik" maxlength="10" /></li>
30 <li>Password: <input type="password" name="password" /></li>
31 
32+# Callable initial data ########################################################
33+
34+The previous technique dealt with raw values as initial data, but it's also
35+possible to specify callable data.
36+
37+>>> class UserRegistration(Form):
38+...    username = CharField(max_length=10)
39+...    password = CharField(widget=PasswordInput)
40+
41+We need to define functions that get called later.
42+>>> def initial_django():
43+...     return 'django'
44+>>> def initial_stephane():
45+...     return 'stephane'
46+
47+Here, we're not submitting any data, so the initial value will be displayed.
48+>>> p = UserRegistration(initial={'username': initial_django}, auto_id=False)
49+>>> print p.as_ul()
50+<li>Username: <input type="text" name="username" value="django" maxlength="10" /></li>
51+<li>Password: <input type="password" name="password" /></li>
52+>>> p = UserRegistration(initial={'username': initial_stephane}, auto_id=False)
53+>>> print p.as_ul()
54+<li>Username: <input type="text" name="username" value="stephane" maxlength="10" /></li>
55+<li>Password: <input type="password" name="password" /></li>
56+
57+The 'initial' parameter is meaningless if you pass data.
58+>>> p = UserRegistration({}, initial={'username': initial_django}, auto_id=False)
59+>>> print p.as_ul()
60+<li><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>
61+<li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>
62+>>> p = UserRegistration({'username': u''}, initial={'username': initial_django}, auto_id=False)
63+>>> print p.as_ul()
64+<li><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>
65+<li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>
66+>>> p = UserRegistration({'username': u'foo'}, initial={'username': initial_django}, auto_id=False)
67+>>> print p.as_ul()
68+<li>Username: <input type="text" name="username" value="foo" maxlength="10" /></li>
69+<li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>
70+
71+A callable 'initial' value is *not* used as a fallback if data is not provided.
72+In this example, we don't provide a value for 'username', and the form raises a
73+validation error rather than using the initial value for 'username'.
74+>>> p = UserRegistration({'password': 'secret'}, initial={'username': initial_django})
75+>>> p.errors
76+{'username': [u'This field is required.']}
77+>>> p.is_valid()
78+False
79+
80+If a Form defines 'initial' *and* 'initial' is passed as a parameter to Form(),
81+then the latter will get precedence.
82+>>> class UserRegistration(Form):
83+...    username = CharField(max_length=10, initial=initial_django)
84+...    password = CharField(widget=PasswordInput)
85+>>> p = UserRegistration(initial={'username': initial_stephane}, auto_id=False)
86+>>> print p.as_ul()
87+<li>Username: <input type="text" name="username" value="stephane" maxlength="10" /></li>
88+<li>Password: <input type="password" name="password" /></li>
89+
90 # Help text ###################################################################
91 
92 You can specify descriptive text for a field by using the 'help_text' argument