form: disable Done button on validation failure of a child form
When the validation of a field fails in a form, we disable the Done button. With child forms, however, it did not work because they have their own set of hidden buttons ; that are not the ones the users interacts with. This patch makes parent forms recurse on the child forms when checking if any field is in error. Also, when a child form undertakes validation, it now propagates to the parent, so that the done button can be immediately enabled/disabled. Having a validation error in a child form that is not currently enabled should not prevent the user from moving forward, so we disable recursion for child forms that are disabled. Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
parent
a9ac5a5fb2
commit
348195b4d6
|
@ -160,6 +160,18 @@ class BoundFormField(object):
|
|||
if isinstance(widget, WantsToKnowFormField):
|
||||
widget.set_bound_form_field(self)
|
||||
|
||||
def is_in_error(self) -> bool:
|
||||
""" Tells whether this field is in error. We will also check if any of
|
||||
the enabled subform reports an error.
|
||||
"""
|
||||
if self.in_error:
|
||||
return True
|
||||
|
||||
if not self._enabled or not isinstance(self.widget, SubFormWidget):
|
||||
return False
|
||||
|
||||
return self.widget.form.has_validation_error()
|
||||
|
||||
def _build_table(self):
|
||||
widget = self.widget
|
||||
if self.field.takes_default_style:
|
||||
|
@ -504,13 +516,12 @@ class Form(object, metaclass=MetaForm):
|
|||
focus_buttons=focus_buttons, excerpt=excerpt,
|
||||
narrow_rows=narrow_rows)
|
||||
|
||||
def has_validation_error(self) -> bool:
|
||||
""" Tells if any field (or subformfield) is in error. """
|
||||
return any(map(lambda f: f.is_in_error(), self._fields))
|
||||
|
||||
def validated(self):
|
||||
in_error = False
|
||||
for f in self._fields:
|
||||
if f.in_error:
|
||||
in_error = True
|
||||
break
|
||||
if in_error:
|
||||
if self.has_validation_error():
|
||||
self.buttons.base_widget.contents[0][0].enabled = False
|
||||
self.buttons.base_widget.focus_position = 1
|
||||
else:
|
||||
|
@ -558,3 +569,8 @@ class SubForm(Form):
|
|||
def __init__(self, parent, **kw):
|
||||
self.parent = parent
|
||||
super().__init__(**kw)
|
||||
|
||||
def validated(self):
|
||||
""" Propagate the validation to the parent. """
|
||||
self.parent.validated()
|
||||
super().validated()
|
||||
|
|
|
@ -0,0 +1,151 @@
|
|||
# Copyright 2022 Canonical, Ltd.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
from subiquitycore.ui.form import (
|
||||
Form,
|
||||
StringField,
|
||||
SubForm,
|
||||
SubFormField,
|
||||
)
|
||||
|
||||
|
||||
class TestForm(TestCase):
|
||||
|
||||
def test_has_validation_error(self):
|
||||
""" Make sure Form.has_validation_form() returns:
|
||||
* True if any field is in error
|
||||
* False otherwise. """
|
||||
class DummyForm(Form):
|
||||
field1 = StringField("DummyStringOne", help="")
|
||||
field2 = StringField("DummyStringTwo", help="")
|
||||
|
||||
form = DummyForm()
|
||||
|
||||
form.field1.in_error = False
|
||||
form.field2.in_error = False
|
||||
self.assertFalse(form.has_validation_error())
|
||||
|
||||
form.field1.in_error = True
|
||||
form.field2.in_error = True
|
||||
self.assertTrue(form.has_validation_error())
|
||||
|
||||
form.field1.in_error = True
|
||||
form.field2.in_error = False
|
||||
self.assertTrue(form.has_validation_error())
|
||||
|
||||
def test_has_validation_error_with_subform(self):
|
||||
""" Make sure Form.has_validation_form() is affected by fields from
|
||||
child forms (only if the child form is enabled). """
|
||||
class DummySubForm(SubForm):
|
||||
field1 = StringField("DummyString", help="")
|
||||
|
||||
class DummyForm(Form):
|
||||
field1 = StringField("DummyString", help="")
|
||||
dummy_subform = SubFormField(DummySubForm, "", help="")
|
||||
|
||||
form = DummyForm()
|
||||
subform = form.dummy_subform.widget.form
|
||||
|
||||
form.field1.in_error = False
|
||||
subform.field1.in_error = False
|
||||
self.assertFalse(form.has_validation_error())
|
||||
|
||||
form.field1.in_error = True
|
||||
subform.field1.in_error = False
|
||||
self.assertTrue(form.has_validation_error())
|
||||
|
||||
form.field1.in_error = False
|
||||
subform.field1.in_error = True
|
||||
self.assertTrue(form.has_validation_error())
|
||||
|
||||
form.field1.in_error = True
|
||||
subform.field1.in_error = True
|
||||
self.assertTrue(form.has_validation_error())
|
||||
|
||||
# Make sure fields in disabled subforms are ignored.
|
||||
form.field1.in_error = False
|
||||
subform.field1.in_error = True
|
||||
form.dummy_subform.enabled = False
|
||||
self.assertFalse(form.has_validation_error())
|
||||
|
||||
def test_has_validation_error_with_subsubform(self):
|
||||
""" Make sure enabling/disabling parent forms also acts as if sub forms
|
||||
are disabled. """
|
||||
class DummySubSubForm(SubForm):
|
||||
field1 = StringField("DummyString", help="")
|
||||
|
||||
class DummySubForm(SubForm):
|
||||
dummy_subform = SubFormField(DummySubSubForm, "", help="")
|
||||
|
||||
class DummyForm(Form):
|
||||
dummy_subform = SubFormField(DummySubForm, "", help="")
|
||||
|
||||
form = DummyForm()
|
||||
subform = form.dummy_subform.widget.form
|
||||
subsubform = subform.dummy_subform.widget.form
|
||||
|
||||
subsubform.field1.in_error = True
|
||||
self.assertTrue(form.has_validation_error())
|
||||
|
||||
# If subsubform is disabled, it should be ignored.
|
||||
subsubform.field1.in_error = True
|
||||
subform.dummy_subform.enabled = False
|
||||
self.assertFalse(form.has_validation_error())
|
||||
|
||||
# If subform is disabled, it should also be ignored.
|
||||
subsubform.field1.in_error = True
|
||||
subform.dummy_subform.enabled = True
|
||||
form.dummy_subform.enabled = False
|
||||
self.assertFalse(form.has_validation_error())
|
||||
|
||||
def test_done_button_auto_toggle(self):
|
||||
""" Make sure calling validated() enables or disables the Done button.
|
||||
"""
|
||||
class DummyForm(Form):
|
||||
field1 = StringField("DummyString", help="")
|
||||
|
||||
form = DummyForm()
|
||||
done_button = form.buttons.base_widget.contents[0][0]
|
||||
|
||||
form.field1.in_error = False
|
||||
form.validated()
|
||||
self.assertTrue(done_button.enabled)
|
||||
|
||||
form.field1.in_error = True
|
||||
form.validated()
|
||||
self.assertFalse(done_button.enabled)
|
||||
|
||||
def test_subform_validated_propagates(self):
|
||||
""" Make sure calling validated() in a subform affects the Done button
|
||||
in the parent form. """
|
||||
class DummySubForm(SubForm):
|
||||
field1 = StringField("DummyString", help="")
|
||||
|
||||
class DummyForm(Form):
|
||||
dummy_subform = SubFormField(DummySubForm, "", help="")
|
||||
|
||||
form = DummyForm()
|
||||
subform = form.dummy_subform.widget.form
|
||||
done_button = form.buttons.base_widget.contents[0][0]
|
||||
|
||||
subform.field1.in_error = False
|
||||
subform.validated()
|
||||
self.assertTrue(done_button.enabled)
|
||||
|
||||
subform.field1.in_error = True
|
||||
subform.validated()
|
||||
self.assertFalse(done_button.enabled)
|
Loading…
Reference in New Issue