Added custom widgets with specific validation rules for integers and floats
authorThierry Florac <thierry.florac@onf.fr>
Fri, 12 Oct 2018 15:04:40 +0200
changeset 153 678b41b062ea
parent 152 0e87c4b2ea02
child 154 92688225ec40
Added custom widgets with specific validation rules for integers and floats
src/pyams_form/widget/__init__.py
src/pyams_form/widget/templates/float-input.pt
src/pyams_form/widget/templates/integer-input.pt
--- a/src/pyams_form/widget/__init__.py	Tue Oct 09 16:18:11 2018 +0200
+++ b/src/pyams_form/widget/__init__.py	Fri Oct 12 15:04:40 2018 +0200
@@ -12,8 +12,6 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
 import inspect
 import json
 import os
@@ -29,26 +27,25 @@
 from z3c.form.browser.textarea import TextAreaWidget
 from z3c.form.button import ButtonAction
 from z3c.form.converter import BaseDataConverter, DatetimeDataConverter as BaseDatetimeDataConverter
-from z3c.form.interfaces import DISPLAY_MODE, IButtonAction, IDataConverter, IFieldWidget, INPUT_MODE, \
+from z3c.form.interfaces import DISPLAY_MODE, IButtonAction, IDataConverter, IFieldWidget, INPUT_MODE, ITextWidget, \
     IWidgetLayoutTemplate, NO_VALUE
 from z3c.form.widget import FieldWidget, WidgetLayoutFactory, WidgetTemplateFactory
 from zope.interface import Interface, directlyProvides, implementer, implementer_only
 from zope.pagetemplate.interfaces import IPageTemplate
 from zope.schema.fieldproperty import FieldProperty
-from zope.schema.interfaces import IChoice, IDate, IDatetime, IList, ISet, ITime, ITuple
+from zope.schema.interfaces import IChoice, IDate, IDatetime, IFloat, IInt, IList, ISet, ITime, ITuple
 
-from pyams_form import _
-# import interfaces
 from pyams_form.interfaces.form import IActionWidget, ICloseWidget, IColorWidget, IDateWidget, IDatetimeWidget, \
     IFormLayer, IHTMLWidget, IResetWidget, ISEOTextLineWidget, ISelect2Widget, ITextLineListWidget, ITimeWidget
 from pyams_form.schema import IActionButton, ICloseButton, IResetButton
 from pyams_skin.interfaces.tinymce import ITinyMCEConfiguration
-# import packages
 from pyams_utils.adapter import adapter_config
 from pyams_utils.interfaces.data import IObjectData
 from pyams_utils.schema import IColorField, IHTMLField, ITextLineListField
 from pyams_utils.timezone import localgmtime, tztime
 
+from pyams_form import _
+
 
 #
 # Widget template configuration
@@ -250,6 +247,54 @@
 
 
 #
+# Integer field widget
+#
+
+@widgettemplate_config(mode=INPUT_MODE, template='templates/integer-input.pt', layer=IFormLayer)
+@implementer_only(ITextWidget)
+class IntegerWidget(TextWidget):
+    """Integer widget"""
+
+    @property
+    def validate_rules(self):
+        rules = {'pattern': '\-?\d+'}
+        if self.field.min is not None:
+            rules['min'] = self.field.min
+        if self.field.max is not None:
+            rules['max'] = self.field.max
+        return json.dumps(rules)
+
+
+@adapter_config(context=(IInt, IFormLayer), provides=IFieldWidget)
+def IntegerFieldWidget(field, request):
+    return FieldWidget(field, IntegerWidget(request))
+
+
+#
+# Float field widget
+#
+
+@widgettemplate_config(mode=INPUT_MODE, template='templates/float-input.pt', layer=IFormLayer)
+@implementer_only(ITextWidget)
+class FloatWidget(TextWidget):
+    """Float widget"""
+
+    @property
+    def validate_rules(self):
+        rules = {'number': True}
+        if self.field.min is not None:
+            rules['min'] = self.field.min
+        if self.field.max is not None:
+            rules['max'] = self.field.max
+        return json.dumps(rules)
+
+
+@adapter_config(context=(IFloat, IFormLayer), provides=IFieldWidget)
+def FloatFieldWidget(field, request):
+    return FieldWidget(field, FloatWidget(request))
+
+
+#
 # Date widget
 #
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_form/widget/templates/float-input.pt	Fri Oct 12 15:04:40 2018 +0200
@@ -0,0 +1,39 @@
+<html xmlns="http://www.w3.org/1999/xhtml"
+	  xmlns:tal="http://xml.zope.org/namespaces/tal"
+	  tal:omit-tag="">
+	<input id="" name="" class="" title="" lang="" disabled=""
+		   readonly="" alt="" tabindex="" accesskey="" size="" maxlength=""
+		   style="" value="" type="text"
+		   data-ams-validate-rules='${view/validate_rules}'
+			tal:attributes='id view/id;
+							name view/name;
+							class view/klass;
+							style view/style;
+							title view/title;
+							lang view/lang;
+							onclick view/onclick;
+							ondblclick view/ondblclick;
+							onmousedown view/onmousedown;
+							onmouseup view/onmouseup;
+							onmouseover view/onmouseover;
+							onmousemove view/onmousemove;
+							onmouseout view/onmouseout;
+							onkeypress view/onkeypress;
+							onkeydown view/onkeydown;
+							onkeyup view/onkeyup;
+							value view/value;
+							disabled view/disabled;
+							tabindex view/tabindex;
+							onfocus view/onfocus;
+							onblur view/onblur;
+							onchange view/onchange;
+							readonly view/readonly;
+							alt view/alt;
+							accesskey view/accesskey;
+							onselect view/onselect;
+							size view/size;
+							maxlength view/maxlength;
+							placeholder view/placeholder;
+							autocapitalize view/autocapitalize;
+							data-ams-data tales:object_data(view);' />
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_form/widget/templates/integer-input.pt	Fri Oct 12 15:04:40 2018 +0200
@@ -0,0 +1,39 @@
+<html xmlns="http://www.w3.org/1999/xhtml"
+	  xmlns:tal="http://xml.zope.org/namespaces/tal"
+	  tal:omit-tag="">
+	<input id="" name="" class="" title="" lang="" disabled=""
+		   readonly="" alt="" tabindex="" accesskey="" size="" maxlength=""
+		   style="" value="" type="text"
+		   data-ams-validate-rules='${view/validate_rules}'
+			tal:attributes='id view/id;
+							name view/name;
+							class view/klass;
+							style view/style;
+							title view/title;
+							lang view/lang;
+							onclick view/onclick;
+							ondblclick view/ondblclick;
+							onmousedown view/onmousedown;
+							onmouseup view/onmouseup;
+							onmouseover view/onmouseover;
+							onmousemove view/onmousemove;
+							onmouseout view/onmouseout;
+							onkeypress view/onkeypress;
+							onkeydown view/onkeydown;
+							onkeyup view/onkeyup;
+							value view/value;
+							disabled view/disabled;
+							tabindex view/tabindex;
+							onfocus view/onfocus;
+							onblur view/onblur;
+							onchange view/onchange;
+							readonly view/readonly;
+							alt view/alt;
+							accesskey view/accesskey;
+							onselect view/onselect;
+							size view/size;
+							maxlength view/maxlength;
+							placeholder view/placeholder;
+							autocapitalize view/autocapitalize;
+							data-ams-data tales:object_data(view);' />
+</html>