diff --git a/src/apps/dashboard/forms.py b/src/apps/dashboard/forms.py new file mode 100644 index 0000000..d227209 --- /dev/null +++ b/src/apps/dashboard/forms.py @@ -0,0 +1,18 @@ +from django import forms +from .models import Item + +EMPTY_ITEM_ERROR = "You can't have an empty list item" + +class ItemForm(forms.models.ModelForm): + class Meta: + model = Item + fields = ("text",) + widgets = { + "text": forms.widgets.TextInput( + attrs={ + "placeholder": "Enter a to-do item", + "class": "form-control form-control-lg", + } + ), + } + error_messages = {"text": {"required": EMPTY_ITEM_ERROR}} diff --git a/src/apps/dashboard/tests/test_forms.py b/src/apps/dashboard/tests/test_forms.py new file mode 100644 index 0000000..93fe4e1 --- /dev/null +++ b/src/apps/dashboard/tests/test_forms.py @@ -0,0 +1,18 @@ +from django.test import TestCase +from ..forms import EMPTY_ITEM_ERROR, ItemForm + +class ItemFormTest(TestCase): + def test_form_item_has_placeholder_and_css_classes(self): + form = ItemForm() + rendered = form.as_p() + self.assertIn('placeholder="Enter a to-do item"', rendered) + self.assertIn('class="form-control form-control-lg"', rendered) + + def test_form_validation_for_blank_items(self): + form = ItemForm(data={"text": ""}) + form.save() + + def test_form_validation_for_blank_items(self): + form = ItemForm(data={"text": ""}) + self.assertFalse(form.is_valid()) + self.assertEqual(form.errors["text"], [EMPTY_ITEM_ERROR])