forms.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. =====================
  2. Using Forms in Misago
  3. =====================
  4. .. note::
  5. Purpose of this document is to introduce you to differences and features of forms module available in misago.core.forms package.
  6. For proper introduction to forums, see `Django documentation <https://docs.djangoproject.com/en/dev/topics/forms/>`_.
  7. Misago extends standard forms library that comes with Django with extra components. First one is `Crispy Forms <http://django-crispy-forms.readthedocs.org/en/latest/>`_ app that allows to use templates to display forms. Thanks to this feature, it's possible to render correct markup that `Bootstrap <getbootstrap.com/css/#forms>`_ requires with minimal amount of effort.
  8. Misago Forms Module
  9. ===================
  10. Second extension lies in :py:mod:`misago.core.forms` module that imports Django forms. This layer introduces tiny convenience change to forms standard clean and validate behaviour that will save you from some "gotchas" associated with input length validation. Unlike other popular frameworks and solutions, Django does not automatically trim input values received from client before validation, which means user can enter few spaces into text fields instead of "readable" value and such input will pass automatical validation and will need additional cleanup and checks in custom ``clean_`` methods. Because there's good chance your app will have plenty of ``CharField`` fields, such boilerplate can quickly add up.
  11. In :py:mod:`misago.core.forms` you will find three classes:
  12. AutoStripWhitespacesMixin
  13. -------------------------
  14. :py:class:`misago.core.forms.AutoStripWhitespacesMixin`
  15. Small mixin that strips whitespaces from all ``CharField``'s input on form, before its passed further for validation.
  16. If you wish to exclude one or more fields from this behaviour, you may add their names to "autostrip_exclude" attribute::
  17. class MyLargeForm(Form):
  18. autostrip_exclude = ['i_wont_be_stripped']
  19. i_will_be_stripped = forms.CharField()
  20. i_wont_be_stripped = forms.CharField()
  21. Form
  22. ----
  23. :py:class:`misago.core.forms.Form`
  24. Wrapper for :py:class:`django.forms.Form` that uses AutoStripWhitespacesMixin.
  25. ModelForm
  26. ---------
  27. :py:class:`misago.core.forms.ModelForm`
  28. Wrapper for :py:class:`django.forms.ModelForm` that uses AutoStripWhitespacesMixin.
  29. YesNoSwitch
  30. -----------
  31. :py:func:`misago.core.forms.YesNoSwitch`
  32. Thin wrapper around Django's ``TypedChoiceField``. This field renders nice yes/no switch as its input.
  33. .. warning::
  34. ``YesNoSwitch`` coerces to ``int``, not to ``bool``! Remember about this when writing code dealing with forms containing this field!
  35. Forums Forms Module
  36. ===================
  37. :py:mod:`misago.forums.forms` module defines two fields you may use for making forum selections in your forms:
  38. ForumChoiceField
  39. ----------------
  40. Extends ``ModelChoiceField``.
  41. ForumsMultipleChoiceField
  42. -------------------------
  43. Extends ``ModelMultipleChoiceField``.
  44. Instead of ``queryset``, both fields expect ``parent`` argument containing ``Forum`` class instance from which forums should be selected. Leave this argument empty for fields to fallback to root category for all categories and forums.
  45. In addition, you can pass field ACL dictionary to further limit choices to forums browserable by ACL owner.
  46. Template Tags
  47. =============
  48. Misago defines custom templates extension named ``misago_forms``. This extension contains two template tags for rendering form fields:
  49. form_row
  50. --------
  51. This tag takes form field as its first argument and renders field complete with label, help and errors. Accept two extra arguments: label class and field class, allowing you to control size of horizontal forms::
  52. {% load misago_forms %}
  53. {% form_row form.somefield %}
  54. {% form_row form.otherfield 'col-md-3' 'col-md-9' %}
  55. form_input
  56. ----------
  57. This tag takes form field as its only argument and renders it's input.