Author: russellm
Date: 2007-05-12 10:50:32 -0500 (Sat, 12 May 2007)
New Revision: 5209

Modified:
   django/trunk/AUTHORS
   django/trunk/django/newforms/forms.py
   django/trunk/tests/regressiontests/forms/tests.py
Log:
Fixed #4018 -- Added code to allow callables as initial data in newforms. 
Thanks for the patch, David Danier.


Modified: django/trunk/AUTHORS
===================================================================
--- django/trunk/AUTHORS        2007-05-12 15:40:46 UTC (rev 5208)
+++ django/trunk/AUTHORS        2007-05-12 15:50:32 UTC (rev 5209)
@@ -78,6 +78,7 @@
     [EMAIL PROTECTED]
     Jure Cuhalev <[EMAIL PROTECTED]>
     [EMAIL PROTECTED]
+    David Danier <[EMAIL PROTECTED]>
     Dirk Datzert <[EMAIL PROTECTED]>
     Jonathan Daugherty (cygnus) <http://www.cprogrammer.org/>
     [EMAIL PROTECTED]

Modified: django/trunk/django/newforms/forms.py
===================================================================
--- django/trunk/django/newforms/forms.py       2007-05-12 15:40:46 UTC (rev 
5208)
+++ django/trunk/django/newforms/forms.py       2007-05-12 15:50:32 UTC (rev 
5209)
@@ -255,6 +255,8 @@
             attrs['id'] = auto_id
         if not self.form.is_bound:
             data = self.form.initial.get(self.name, self.field.initial)
+            if callable(data):
+                data = data()
         else:
             data = self.data
         return widget.render(self.html_name, data, attrs=attrs)

Modified: django/trunk/tests/regressiontests/forms/tests.py
===================================================================
--- django/trunk/tests/regressiontests/forms/tests.py   2007-05-12 15:40:46 UTC 
(rev 5208)
+++ django/trunk/tests/regressiontests/forms/tests.py   2007-05-12 15:50:32 UTC 
(rev 5209)
@@ -2752,6 +2752,64 @@
 <li>Username: <input type="text" name="username" value="babik" maxlength="10" 
/></li>
 <li>Password: <input type="password" name="password" /></li>
 
+# Callable initial data 
########################################################
+
+The previous technique dealt with raw values as initial data, but it's also
+possible to specify callable data.
+
+>>> class UserRegistration(Form):
+...    username = CharField(max_length=10)
+...    password = CharField(widget=PasswordInput)
+
+We need to define functions that get called later.
+>>> def initial_django():
+...     return 'django'
+>>> def initial_stephane():
+...     return 'stephane'
+
+Here, we're not submitting any data, so the initial value will be displayed.
+>>> p = UserRegistration(initial={'username': initial_django}, auto_id=False)
+>>> print p.as_ul()
+<li>Username: <input type="text" name="username" value="django" maxlength="10" 
/></li>
+<li>Password: <input type="password" name="password" /></li>
+
+The 'initial' parameter is meaningless if you pass data.
+>>> p = UserRegistration({}, initial={'username': initial_django}, 
auto_id=False)
+>>> print p.as_ul()
+<li><ul class="errorlist"><li>This field is required.</li></ul>Username: 
<input type="text" name="username" maxlength="10" /></li>
+<li><ul class="errorlist"><li>This field is required.</li></ul>Password: 
<input type="password" name="password" /></li>
+>>> p = UserRegistration({'username': u''}, initial={'username': 
initial_django}, auto_id=False)
+>>> print p.as_ul()
+<li><ul class="errorlist"><li>This field is required.</li></ul>Username: 
<input type="text" name="username" maxlength="10" /></li>
+<li><ul class="errorlist"><li>This field is required.</li></ul>Password: 
<input type="password" name="password" /></li>
+>>> p = UserRegistration({'username': u'foo'}, initial={'username': 
initial_django}, auto_id=False)
+>>> print p.as_ul()
+<li>Username: <input type="text" name="username" value="foo" maxlength="10" 
/></li>
+<li><ul class="errorlist"><li>This field is required.</li></ul>Password: 
<input type="password" name="password" /></li>
+
+A callable 'initial' value is *not* used as a fallback if data is not provided.
+In this example, we don't provide a value for 'username', and the form raises a
+validation error rather than using the initial value for 'username'.
+>>> p = UserRegistration({'password': 'secret'}, initial={'username': 
initial_django})
+>>> p.errors
+{'username': [u'This field is required.']}
+>>> p.is_valid()
+False
+
+If a Form defines 'initial' *and* 'initial' is passed as a parameter to Form(),
+then the latter will get precedence.
+>>> class UserRegistration(Form):
+...    username = CharField(max_length=10, initial=initial_django)
+...    password = CharField(widget=PasswordInput)
+>>> p = UserRegistration(auto_id=False)
+>>> print p.as_ul()
+<li>Username: <input type="text" name="username" value="django" maxlength="10" 
/></li>
+<li>Password: <input type="password" name="password" /></li>
+>>> p = UserRegistration(initial={'username': initial_stephane}, auto_id=False)
+>>> print p.as_ul()
+<li>Username: <input type="text" name="username" value="stephane" 
maxlength="10" /></li>
+<li>Password: <input type="password" name="password" /></li>
+
 # Help text ###################################################################
 
 You can specify descriptive text for a field by using the 'help_text' argument


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to