Skip to content

Keep composite field values after a validation error #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions postgres_composite_types/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ def __init__(self, *args, fields=None, model=None, **kwargs):
self.widget.widgets.values()):
widget.attrs['placeholder'] = field.label

def prepare_value(self, value):
"""
Prepare the field data for the CompositeTypeWidget, which expects data
as a dict.
"""
if isinstance(value, CompositeType):
return value.__to_dict__()

return value

def validate(self, value):
pass

Expand Down Expand Up @@ -155,7 +165,9 @@ def get_bound_field(self, form, field_name):

class CompositeTypeWidget(forms.Widget):
"""
Takes an ordered dict of widgets to produce a composite form widget
Takes an ordered dict of widgets to produce a composite form widget. This
widget knows nothing about CompositeTypes, and works only with dicts for
initial and output data.
"""
template_name = \
'postgres_composite_types/forms/widgets/composite_type.html'
Expand All @@ -170,7 +182,7 @@ def __init__(self, widgets, **kwargs):

@property
def is_hidden(self):
return all(w.is_hidden for w in self.widgets)
return all(w.is_hidden for w in self.widgets.values())

def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
Expand All @@ -187,12 +199,9 @@ def get_context(self, name, value, attrs):
if id_:
widget_attrs['id'] = '%s-%s' % (id_, subname)

subwidgets[subname] = widget.render('%s-%s' % (name, subname),
getattr(value, subname, None),
final_attrs)
widget_context = widget.get_context(
'%s-%s' % (name, subname),
getattr(value, subname, None),
value.get(subname),
widget_attrs)
subwidgets[subname] = widget_context['widget']

Expand Down
45 changes: 45 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from django import forms
from django.test import SimpleTestCase
from django.test.testcases import assert_and_parse_html

from postgres_composite_types.forms import CompositeTypeField

Expand Down Expand Up @@ -56,6 +57,14 @@ def test_validation(self):
self.assertEqual(str(form.errors['simple_field'][0]),
'A number: Enter a whole number.')

# Fields with validation errors should render with their invalid input
self.assertHTMLContains(
"""
<input id="id_simple_field-a" name="simple_field-a"
placeholder="A number" required type="number" value="one" />
""",
str(form['simple_field']))

def test_subfield_validation(self):
"""Errors on subfields should be accessible"""
form = self.SimpleForm(data={
Expand Down Expand Up @@ -85,6 +94,42 @@ def test_nested_prefix(self):
self.assertEqual(a_bound_field.html_name,
'step1-simple_field-a')

def test_initial_data(self):
"""
Check that forms with initial data render with the fields prepopulated.
"""
initial = SimpleType(
a=1, b='foo', c=datetime.datetime(2016, 5, 24, 17, 38, 32))
form = self.SimpleForm(initial={'simple_field': initial})

self.assertHTMLContains(
"""
<input id="id_simple_field-a" name="simple_field-a"
placeholder="A number" required type="number" value="1" />
""",
str(form['simple_field']))

# pylint:disable=invalid-name
def assertHTMLContains(self, text, content, count=None, msg=None):
"""
Assert that the HTML snippet ``text`` is found within the HTML snippet
``content``. Like assertContains, but works with plain strings instead
of Response instances.
"""
content = assert_and_parse_html(
self, content, None, "HTML content to search in is not valid:")
text = assert_and_parse_html(
self, text, None, "HTML content to search for is not valid:")

matches = content.count(text)
if count is None:
self.assertTrue(
matches > 0, msg=msg or 'Could not find HTML snippet')
else:
self.assertEqual(
matches, count,
msg=msg or 'Found %d matches, expecting %d' % (matches, count))


class OptionalFieldTests(SimpleTestCase):
"""
Expand Down