from django import newforms as forms
from django.newforms.forms import BoundField

class HiddenBaseForm(forms.BaseForm):
    """
    Adds an ``as_hidden`` method to forms, which allows you to render
    a form using ``<input type="hidden">`` elements for every field.

    There is no error checking or display, as it is assumed you will
    be rendering a pre-validated form for resubmission, for example -
    when generating a preview of something based on a valid form's
    contents, resubmitting the same form via POST as confirmation.
    """
    def as_hidden(self):
        """
        Returns this form rendered entirely as hidden fields.
        """
        output = []
        for name, field in self.fields.items():
            bf = BoundField(self, field, name)
            output.append(bf.as_hidden())
        return u'\n'.join(output)