Skip to content

Commit

Permalink
- TextLinesConverter: do not ignore newlines at the end of the inpu…
Browse files Browse the repository at this point in the history
…tted

  string, thus do not eat blank items
- `TextLinesConverter`: toFieldValue, convert conversion exceptions to
  `FormatterValidationError`, for cases like got a string instead of int
  • Loading branch information
Adam Groszer committed Nov 24, 2012
1 parent 884e1ec commit b03edf1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGES.txt
Expand Up @@ -20,6 +20,12 @@ CHANGES
has been applied also to the existing translations (where
applicable).

- `TextLinesConverter`: do not ignore newlines at the end of the inputted
string, thus do not eat blank items

- `TextLinesConverter`: toFieldValue, convert conversion exceptions to
`FormatterValidationError`, for cases like got a string instead of int

2.9.0 (2012-09-17)
------------------

Expand Down
14 changes: 12 additions & 2 deletions src/z3c/form/converter.py
Expand Up @@ -300,7 +300,7 @@ def toWidgetValue(self, value):
for entry in value:
try:
values.append(widget.terms.getTerm(entry).token)
except LookupError, err:
except LookupError:
# Swallow lookup errors, in case the options changed.
pass
return values
Expand Down Expand Up @@ -339,7 +339,17 @@ def toFieldValue(self, value):
valueType = self.field.value_type._type
if isinstance(valueType, tuple):
valueType = valueType[0]
return collectionType(valueType(v) for v in value.splitlines())
# having a blank line at the end matters, one might want to have a blank
# entry at the end, resp. do not eat it once we have one
# splitlines ate that, so need to use split now
value = value.replace('\r\n', '\n')
items = []
for v in value.split('\n'):
try:
items.append(valueType(v))
except ValueError, err:
raise FormatterValidationError(str(err), v)
return collectionType(items)


class MultiConverter(BaseDataConverter):
Expand Down
29 changes: 27 additions & 2 deletions src/z3c/form/converter.txt
Expand Up @@ -897,6 +897,11 @@ We can now convert a real value to a widget value:
>>> tlc.toWidgetValue([u'de', u'fr', u'en'])
u'de\nfr\nen'

Empty entries are significant:

>>> tlc.toWidgetValue([u'de', u'fr', u'en', u''])
u'de\nfr\nen\n'


The result is always a string, since text lines widgets only deal with textarea
as input field. Of course, we can convert the widget value back to an internal
Expand All @@ -910,6 +915,17 @@ Each line should be one item:
>>> tlc.toFieldValue('this morning\ntomorrow evening\nyesterday')
[u'this morning', u'tomorrow evening', u'yesterday']

Empty lines are significant:

>>> tlc.toFieldValue('de\n\nfr\nen')
[u'de', u'', u'fr', u'en']

Empty lines are also significant at the end:

>>> tlc.toFieldValue('de\nfr\nen\n')
[u'de', u'fr', u'en', u'']


An empty string will also cause the missing value to be returned:

>>> tlc.toFieldValue('') is None
Expand Down Expand Up @@ -939,7 +955,7 @@ Of course, it still can convert to the widget value:

And back:

>>> tlc.toFieldValue(u'1\n2\n3\n')
>>> tlc.toFieldValue(u'1\n2\n3')
[1, 2, 3]

An empty string will also cause the missing value to be returned:
Expand Down Expand Up @@ -973,9 +989,18 @@ Of course, it still can convert to the widget value:

And back:

>>> tlc.toFieldValue(u'1\n2\n3\n')
>>> tlc.toFieldValue(u'1\n2\n3')
(1, 2, 3)

What if we have a wrong number:

>>> tlc.toFieldValue(u'1\n2\n3\nfoo')
Traceback (most recent call last):
...
FormatterValidationError: ("invalid literal for int() with base 10: 'foo'", u'foo')




Multi Data Converter
--------------------
Expand Down

0 comments on commit b03edf1

Please sign in to comment.