From a807cf4f57bd0bd68899ced2f606d383cde10f8e Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Fri, 10 Feb 2017 01:04:46 +0100 Subject: [PATCH] Do not show the Clear button for required Date or DateTime fields. --- CHANGES.rst | 3 ++- plone/app/z3cform/tests/test_widgets.py | 17 +++++++++++++++++ plone/app/z3cform/widget.py | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index bcb29c7d..73794301 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,7 +10,8 @@ Breaking changes: New features: -- *add item here* +- Do not show the "Clear" button for required Date or DateTime fields. + [thet] Bug fixes: diff --git a/plone/app/z3cform/tests/test_widgets.py b/plone/app/z3cform/tests/test_widgets.py index 9cbd0954..61a3e018 100644 --- a/plone/app/z3cform/tests/test_widgets.py +++ b/plone/app/z3cform/tests/test_widgets.py @@ -148,6 +148,7 @@ def setUp(self): self.request = TestRequest(environ={'HTTP_ACCEPT_LANGUAGE': 'en'}) self.field = Date(__name__='datefield') + self.field.required = False self.widget = DateWidget(self.request) self.widget.field = self.field self.widget.pattern_options = {'date': {'firstDay': 0}} @@ -188,6 +189,13 @@ def test_widget(self): self.widget._base_args(), ) + def test_widget_required(self): + """Required fields should not have a "Clear" button. + """ + self.field.required = True + base_args = self.widget._base_args() + self.assertEqual(base_args['pattern_options']['clear'], False) + def test_data_converter(self): from plone.app.z3cform.widget import DateWidgetConverter converter = DateWidgetConverter(self.field, self.widget) @@ -240,7 +248,9 @@ def setUp(self): self.request = TestRequest(environ={'HTTP_ACCEPT_LANGUAGE': 'en'}) self.field = Datetime(__name__='datetimefield') + self.field.required = False self.widget = DatetimeWidget(self.request) + self.widget.field = self.field self.widget.pattern_options = { 'date': {'firstDay': 0}, 'time': {'interval': 15} @@ -286,6 +296,13 @@ def test_widget(self): self.widget._base_args(), ) + def test_widget_required(self): + """Required fields should not have a "Clear" button. + """ + self.field.required = True + base_args = self.widget._base_args() + self.assertEqual(base_args['pattern_options']['clear'], False) + def test_data_converter(self): from plone.app.z3cform.widget import DatetimeWidgetConverter converter = DatetimeWidgetConverter(self.field, self.widget) diff --git a/plone/app/z3cform/widget.py b/plone/app/z3cform/widget.py index 56cec146..9d8406ee 100644 --- a/plone/app/z3cform/widget.py +++ b/plone/app/z3cform/widget.py @@ -135,6 +135,9 @@ def _base_args(self): self.value) or u'').strip() args.setdefault('pattern_options', {}) + if self.field.required: + # Required fields should not have a "Clear" button + args['pattern_options']['clear'] = False args['pattern_options'] = dict_merge( get_date_options(self.request), args['pattern_options'])