Django

Code

Changeset 9569

Show
Ignore:
Timestamp:
12/04/08 22:22:00 (1 month ago)
Author:
mtredinnick
Message:

Added a way to iterate over hidden/visible fields in a form. Useful for manual
form layout.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/forms/forms.py

    r9365 r9569  
    304304        return False 
    305305 
     306    def hidden_fields(self): 
     307        """ 
     308        Returns a list of all the BoundField objects that correspond to hidden 
     309        fields in the HTML output. Useful for manual form layout in templates. 
     310        """ 
     311        return [field for field in self if field.is_hidden] 
     312 
     313    def visible_fields(self): 
     314        """ 
     315        Returns a lits of BoundField objects that do not correspond to hidden 
     316        fields. The opposite of the hidden_fields() method. 
     317        """ 
     318        return [field for field in self if not field.is_hidden] 
     319 
    306320class Form(BaseForm): 
    307321    "A collection of Fields, plus their associated data." 
     
    364378            name = self.html_initial_name 
    365379        return widget.render(name, data, attrs=attrs) 
    366          
     380 
    367381    def as_text(self, attrs=None, **kwargs): 
    368382        """ 
  • django/trunk/docs/topics/forms/index.txt

    r9517 r9569  
    293293        message. 
    294294 
     295    ``field.is_hidden`` 
     296        This attribute is ``True`` is the form field is a hidden field and 
     297        ``False`` otherwise. It's not particularly useful as a template 
     298        variable, but could be useful in conditional tests such as:: 
     299 
     300            {% if field.is_hidden %} 
     301               {# Do something special #} 
     302            {% endif %} 
     303 
     304Looping over hidden and visible fields 
     305~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     306 
     307If you are manually laying out a form in a template, you will often want to 
     308work with any hidden fields in a single loop and then treat the visible fields 
     309differently. For example, since hidden fields don't display anything, putting 
     310their error messages "next to" the field isn't going to be very clear to the 
     311reader. So you need to handle errors for those fields a bit differently. 
     312 
     313Django provides two methods on a form that allow you to loop over the hidden 
     314and visible fields independently: ``hidden_fields()`` and 
     315``visible_fields()``. In a template, you might use these like this (this is a 
     316modification of an earlier example):: 
     317 
     318    <form action="/contact/" method="POST"> 
     319        {% for field in form.visible_fields %} 
     320            <div class="fieldWrapper"> 
     321 
     322                {# Include the hidden fields in the form #} 
     323                {% if forloop.first %} 
     324                    {% for hidden in form.hidden_fields %} 
     325                    {{ field }} 
     326                    {% endfor %} 
     327                {% endif %} 
     328 
     329                {{ field.errors }} 
     330                {{ field.label_tag }}: {{ field }} 
     331            </div> 
     332        {% endfor %} 
     333        <p><input type="submit" value="Send message" /></p> 
     334    </form> 
     335 
     336This example does not handle any errors in the hidden fields. Usually, an 
     337error in a hidden field is a sign of form tampering, since normal form 
     338interaction won't alter them. However, you could easily insert some error 
     339displays for those form errors as well. 
     340 
     341.. versionadded:: 1.1 
     342    The ``hidden_fields`` and ``visible_fields`` methods are new in Django 
     343    1.1. 
     344 
    295345Reusable form templates 
    296346----------------------- 
  • django/trunk/tests/regressiontests/forms/forms.py

    r9365 r9569  
    17581758True 
    17591759 
     1760# Extracting hidden and visible fields ###################################### 
     1761 
     1762>>> class SongForm(Form): 
     1763...     token = CharField(widget=HiddenInput) 
     1764...     artist = CharField() 
     1765...     name = CharField() 
     1766>>> form = SongForm() 
     1767>>> [f.name for f in form.hidden_fields()] 
     1768['token'] 
     1769>>> [f.name for f in form.visible_fields()] 
     1770['artist', 'name'] 
     1771 
    17601772"""