fix: display "global" errors at top of form, if present
this probably could use more work, good enough for now
This commit is contained in:
parent
448dc9fc79
commit
6e4f390f3f
|
@ -1111,6 +1111,32 @@ class Form:
|
|||
|
||||
return self.validated
|
||||
|
||||
def has_global_errors(self):
|
||||
"""
|
||||
Convenience function to check if the form has any "global"
|
||||
(not field-level) errors.
|
||||
|
||||
See also :meth:`get_global_errors()`.
|
||||
|
||||
:returns: ``True`` if global errors present, else ``False``.
|
||||
"""
|
||||
dform = self.get_deform()
|
||||
return bool(dform.error)
|
||||
|
||||
def get_global_errors(self):
|
||||
"""
|
||||
Returns a list of "global" (not field-level) error messages
|
||||
for the form.
|
||||
|
||||
See also :meth:`has_global_errors()`.
|
||||
|
||||
:returns: List of error messages (possibly empty).
|
||||
"""
|
||||
dform = self.get_deform()
|
||||
if dform.error is None:
|
||||
return []
|
||||
return dform.error.messages()
|
||||
|
||||
def get_field_errors(self, field):
|
||||
"""
|
||||
Return a list of error messages for the given field.
|
||||
|
|
|
@ -4,6 +4,14 @@
|
|||
${h.form(form.action_url, method='post', enctype='multipart/form-data', **form_attrs)}
|
||||
${h.csrf_token(request)}
|
||||
|
||||
% if form.has_global_errors():
|
||||
% for msg in form.get_global_errors():
|
||||
<b-notification type="is-warning" :closable="false">
|
||||
${msg}
|
||||
</b-notification>
|
||||
% endfor
|
||||
% endif
|
||||
|
||||
<section>
|
||||
% for fieldname in form:
|
||||
${form.render_vue_field(fieldname)}
|
||||
|
|
|
@ -544,6 +544,34 @@ class TestForm(TestCase):
|
|||
data = form.get_vue_model_data()
|
||||
self.assertEqual(list(data.values()), ['one', 'two', True])
|
||||
|
||||
def test_has_global_errors(self):
|
||||
|
||||
def fail(node, value):
|
||||
node.raise_invalid("things are bad!")
|
||||
|
||||
schema = self.make_schema()
|
||||
schema.validator = fail
|
||||
form = self.make_form(schema=schema)
|
||||
self.assertFalse(form.has_global_errors())
|
||||
self.request.method = 'POST'
|
||||
self.request.POST = {'foo': 'one', 'bar': 'two'}
|
||||
self.assertFalse(form.validate())
|
||||
self.assertTrue(form.has_global_errors())
|
||||
|
||||
def test_get_global_errors(self):
|
||||
|
||||
def fail(node, value):
|
||||
node.raise_invalid("things are bad!")
|
||||
|
||||
schema = self.make_schema()
|
||||
schema.validator = fail
|
||||
form = self.make_form(schema=schema)
|
||||
self.assertEqual(form.get_global_errors(), [])
|
||||
self.request.method = 'POST'
|
||||
self.request.POST = {'foo': 'one', 'bar': 'two'}
|
||||
self.assertFalse(form.validate())
|
||||
self.assertTrue(form.get_global_errors(), ["things are bad!"])
|
||||
|
||||
def test_get_field_errors(self):
|
||||
schema = self.make_schema()
|
||||
|
||||
|
|
Loading…
Reference in a new issue