Ticket #12856: bound-field-required.diff

File bound-field-required.diff, 1.5 KB (added by Petr Marhoun <petr.marhoun@…>, 14 years ago)
  • django/forms/forms.py

    diff --git a/django/forms/forms.py b/django/forms/forms.py
    a b  
    502502        return self.field.widget.is_hidden
    503503    is_hidden = property(_is_hidden)
    504504
     505    def _required(self):
     506        "Returns True if this BoundField's field is required."
     507        return self.field.required
     508    required = property(_required)
     509
    505510    def _auto_id(self):
    506511        """
    507512        Calculates and returns the ID attribute for this BoundField, if the
  • tests/regressiontests/forms/forms.py

    diff --git a/tests/regressiontests/forms/forms.py b/tests/regressiontests/forms/forms.py
    a b  
    17391739<input type="submit" />
    17401740</form>
    17411741
     1742Use form.[field].required to identify required fields.
     1743>>> class OptionalPersonForm(Form):
     1744...     first_name = CharField()
     1745...     last_name = CharField()
     1746...     nick_name = CharField(required=False)
     1747>>> t = Template('<p class="{% if field.required %}required{% else %}optional{% endif %}"><label>{{ field.label }}: {{ field }}</label></p>')
     1748>>> for boundfield in OptionalPersonForm(auto_id=False):
     1749...     print t.render(Context({'field': boundfield}))
     1750<p class="required"><label>First name: <input type="text" name="first_name" /></label></p>
     1751<p class="required"><label>Last name: <input type="text" name="last_name" /></label></p>
     1752<p class="optional"><label>Nick name: <input type="text" name="nick_name" /></label></p>
    17421753
    17431754# The empty_permitted attribute ##############################################
    17441755
Back to Top