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@…>, 17 years ago)

Patch including tests

  • django/newforms/forms.py

    ==== Patch <django-make-initial-newforms-value-callable> level 1
    Source: f4626db1-382e-0410-8594-a82d82b39f71:/make-initial-newforms-value-callable/trunk:4942 [local]
    Target: bcc190cf-cafb-0310-a4f2-bffc1f526a37:/django/trunk:5061 [mirrored]
            (http://code.djangoproject.com/svn/django)
    Log:
     r4875@damasonium:  ddanier | 2007-04-16 14:25:37 +0200
      * Initial value for newforms-fields may be callable now
     r4942@damasonium:  ddanier | 2007-04-23 10:14:39 +0200
      * Tests for callable initial data
    
    === django/newforms/forms.py
    ==================================================================
     
    248248            attrs['id'] = auto_id
    249249        if not self.form.is_bound:
    250250            data = self.form.initial.get(self.name, self.field.initial)
     251            if callable(data):
     252                data = data()
    251253        else:
    252254            data = self.data
    253255        return widget.render(self.html_name, data, attrs=attrs)
  • tests/regressiontests/forms/tests.py

    === tests/regressiontests/forms/tests.py
    ==================================================================
     
    27042704<li>Username: <input type="text" name="username" value="babik" maxlength="10" /></li>
    27052705<li>Password: <input type="password" name="password" /></li>
    27062706
     2707# Callable initial data ########################################################
     2708
     2709The previous technique dealt with raw values as initial data, but it's also
     2710possible to specify callable data.
     2711
     2712>>> class UserRegistration(Form):
     2713...    username = CharField(max_length=10)
     2714...    password = CharField(widget=PasswordInput)
     2715
     2716We need to define functions that get called later.
     2717>>> def initial_django():
     2718...     return 'django'
     2719>>> def initial_stephane():
     2720...     return 'stephane'
     2721
     2722Here, we're not submitting any data, so the initial value will be displayed.
     2723>>> p = UserRegistration(initial={'username': initial_django}, auto_id=False)
     2724>>> print p.as_ul()
     2725<li>Username: <input type="text" name="username" value="django" maxlength="10" /></li>
     2726<li>Password: <input type="password" name="password" /></li>
     2727>>> p = UserRegistration(initial={'username': initial_stephane}, auto_id=False)
     2728>>> print p.as_ul()
     2729<li>Username: <input type="text" name="username" value="stephane" maxlength="10" /></li>
     2730<li>Password: <input type="password" name="password" /></li>
     2731
     2732The 'initial' parameter is meaningless if you pass data.
     2733>>> p = UserRegistration({}, initial={'username': initial_django}, auto_id=False)
     2734>>> print p.as_ul()
     2735<li><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>
     2736<li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>
     2737>>> p = UserRegistration({'username': u''}, initial={'username': initial_django}, auto_id=False)
     2738>>> print p.as_ul()
     2739<li><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>
     2740<li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>
     2741>>> p = UserRegistration({'username': u'foo'}, initial={'username': initial_django}, auto_id=False)
     2742>>> print p.as_ul()
     2743<li>Username: <input type="text" name="username" value="foo" maxlength="10" /></li>
     2744<li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>
     2745
     2746A callable 'initial' value is *not* used as a fallback if data is not provided.
     2747In this example, we don't provide a value for 'username', and the form raises a
     2748validation error rather than using the initial value for 'username'.
     2749>>> p = UserRegistration({'password': 'secret'}, initial={'username': initial_django})
     2750>>> p.errors
     2751{'username': [u'This field is required.']}
     2752>>> p.is_valid()
     2753False
     2754
     2755If a Form defines 'initial' *and* 'initial' is passed as a parameter to Form(),
     2756then the latter will get precedence.
     2757>>> class UserRegistration(Form):
     2758...    username = CharField(max_length=10, initial=initial_django)
     2759...    password = CharField(widget=PasswordInput)
     2760>>> p = UserRegistration(initial={'username': initial_stephane}, auto_id=False)
     2761>>> print p.as_ul()
     2762<li>Username: <input type="text" name="username" value="stephane" maxlength="10" /></li>
     2763<li>Password: <input type="password" name="password" /></li>
     2764
    27072765# Help text ###################################################################
    27082766
    27092767You can specify descriptive text for a field by using the 'help_text' argument
Back to Top