Formulator acts funny for 2-character options
Hi Martijn & all This code, context.REQUEST.SESSION.set('state_evaluate_items', ['yes', 'no']) in a state helper for this field:: evaluate = fields.RadioField('evaluate', title='Please contact me for a full evaluation') evaluate.tales['default'] = "form/REQUEST/SESSION/state_evaluate" evaluate.tales['items'] = "form/REQUEST/SESSION/state_evaluate_items" causes this HTML to be rendered:: <input value="yes" name="field_evaluate" type="radio" />yes<br /><input value="o" name="field_evaluate" type="radio" />n This is also the case for MultiCheckBoxFields and other fields with items. I'm assuming it has to do with this behaviour:: A list item can also be a tuple consisting " "of two elements. The first element of the tuple should be a string " "that is name of the item that should be displayed. The second " "element of the tuple should be the value that will be submitted. and that a 2-char string is misrecognized as a 2-item tuple, but I can't figure out where in the code it occurs. Where should I start looking? I'm using Formulator-1.4.1, but I did a diff with CVS HEAD and didn't notice anything that looked like it addresses this issue. -- Jean Jordaan http://www.upfrontsystems.co.za
Hi there This fixes it :) I'm sure it could be more elegant, but you get the idea .. --- /usr/local/zope/Products/Formulator-1.4.1/Widget.py 2003-07-04 12:47:48.000000000 +0200 +++ Formulator/Widget.py 2003-08-18 18:42:12.000000000 +0200 @@ -391,12 +391,17 @@ # first one only (for now, may be able to fix this better later) selected_found = 0 rendered_items = [] + from types import StringType for item in items: - try: - item_text, item_value = item - except ValueError: + if isinstance(item, StringType): item_text = item item_value = item + else: + try: + item_text, item_value = item + except ValueError: + item_text = item + item_value = item if item_value == value and not selected_found: -- Jean Jordaan http://www.upfrontsystems.co.za
Hmm, was a bit hasty. The fix is needed in a couple of places in Widget.py and Validator.py .. New patch attached. -- Jean Jordaan http://www.upfrontsystems.co.za diff --exclude='*.pyc' --exclude='*~' --exclude='.*' -ru /usr/local/zope/Products/Formulator-1.4.1/Validator.py Formulator/Validator.py --- /usr/local/zope/Products/Formulator-1.4.1/Validator.py 2003-05-16 17:44:47.000000000 +0200 +++ Formulator/Validator.py 2003-08-19 11:10:14.000000000 +0200 @@ -7,6 +7,19 @@ from urlparse import urljoin from Errors import ValidationError +def _splitItem(item): + from types import StringType + if isinstance(item, StringType): + item_text = item + item_value = item + else: + try: + item_text, item_value = item + except ValueError: + item_text = item + item_value = item + return item_text, item_value + class Validator: """Validates input and possibly transforms it to output. """ @@ -366,11 +379,7 @@ # get the text and the value from the list of items for item in field.get_value('items'): - try: - item_text, item_value = item - except ValueError: - item_text = item - item_value = item + item_text, item_value = _splitItem(item) # check if the value is equal to the string/unicode version of # item_value; if that's the case, we can return the *original* @@ -435,11 +444,7 @@ # create a dictionary of possible values value_dict = {} for item in field.get_value('items'): - try: - item_text, item_value = item - except ValueError: - item_text = item - item_value = item + item_text, item_value = _splitItem(item) value_dict[item_value] = 0 # check whether all values are in dictionary @@ -677,4 +682,3 @@ DateTimeValidatorInstance = DateTimeValidator() - diff --exclude='*.pyc' --exclude='*~' --exclude='.*' -ru /usr/local/zope/Products/Formulator-1.4.1/Widget.py Formulator/Widget.py --- /usr/local/zope/Products/Formulator-1.4.1/Widget.py 2003-07-04 12:47:48.000000000 +0200 +++ Formulator/Widget.py 2003-08-19 11:05:53.000000000 +0200 @@ -3,6 +3,19 @@ from DocumentTemplate.DT_Util import html_quote from DateTime import DateTime +def _splitItem(item): + from types import StringType + if isinstance(item, StringType): + item_text = item + item_value = item + else: + try: + item_text, item_value = item + except ValueError: + item_text = item + item_value = item + return item_text, item_value + class Widget: """A field widget that knows how to display itself as HTML. """ @@ -392,12 +405,7 @@ selected_found = 0 rendered_items = [] for item in items: - try: - item_text, item_value = item - except ValueError: - item_text = item - item_value = item - + item_text, item_value = _splitItem(item) if item_value == value and not selected_found: rendered_item = self.render_selected_item(item_text, @@ -448,11 +456,7 @@ css_class = field.get_value('css_class') rendered_items = [] for item in items: - try: - item_text, item_value = item - except ValueError: - item_text = item - item_value = item + item_text, item_value = _splitItem(item) if item_value in value: rendered_item = self.render_selected_item(item_text, @@ -476,11 +480,7 @@ items = field.get_value('items') d = {} for item in items: - try: - item_text, item_value = item - except ValueError: - item_text = item - item_value = item + item_text, item_value = _splitItem(item) d[item_value] = item_text result = [] for e in value: @@ -885,6 +885,3 @@ else: return apply(render_tag, (tag,), kw) + " />" - - -
participants (1)
-
Jean Jordaan