Skip to content

Commit

Permalink
Fix value_label property (getter) in selectComponent, if no value is …
Browse files Browse the repository at this point in the history
…set.

Added a unittest for this case.

WARNING - Backward incompatibility:
This also changes value_label return value from False to None if no value is set.
So check whether to update any usage (code).
However no need when None is used in comparisons (if etc), which converts to boolean False.
  • Loading branch information
bobslee committed Aug 28, 2024
1 parent c501ab6 commit 45a1789
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# Changelog

## 2.1.0

Fix `value_label` property (getter) in `selectComponent`, if no value is set.

**WARNING - Backward incompatibility**\
This also changes `value_label` return value from `False` to `None` if no value is set.\
So check whether to update any usage (code).\
However no need when `None` is used in comparisons (`if` etc), which converts to boolean `False`.

## 2.0.3

Add `dataSrc` getter (property) in `selectboxesComponent`.
Add `dataSrc` property (getter) in `selectboxesComponent`.

## 2.0.2

Expand Down
4 changes: 3 additions & 1 deletion formiodata/components/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def multiple(self):

@property
def value_label(self):
if not self.value:
return None
comp = self.component_owner.input_components.get(self.key)
if self.dataSrc == 'url':
label = self.value['label']
Expand All @@ -39,7 +41,7 @@ def value_label(self):
else:
return label
else:
return False
return None

@property
def value_labels(self):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "formio-data"
version = "2.0.3"
version = "2.1.0"
homepage = "https://github.com/novacode-nl/python-formio-data"
description = "formio.js JSON-data API"
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions tests/test_component_select_multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ def test_get_form(self):
self.assertEqual(food.label, 'Favourite Food')
self.assertEqual(food.value, ['mexican', 'chinese'])
self.assertEqual(food.value_labels, ['Mexican', 'Chinese'])
self.assertEqual(food.value_label, False)
self.assertEqual(food.value_label, None)
self.assertEqual(food.type, 'select')

def test_get_form_data(self):
food = self.form.input.favouriteFood
self.assertEqual(food.label, 'Favourite Food')
self.assertEqual(food.value, ['mexican', 'chinese'])
self.assertEqual(food.value_labels, ['Mexican', 'Chinese'])
self.assertEqual(food.value_label, False)
self.assertEqual(food.value_label, None)
self.assertEqual(food.type, 'select')

# i18n translations
Expand Down
9 changes: 9 additions & 0 deletions tests/test_component_select_one.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ def test_get_form_data(self):
self.assertEqual(season.value_label, 'Autumn')
self.assertEqual(season.type, 'select')

def test_empty_value(self):
season = self.form.input_components['favouriteSeason']
self.assertEqual(season.label, 'Favourite Season')
self.assertEqual(season.value, 'autumn')
season.value = None
self.assertEqual(season.value, None)
self.assertEqual(season.value_label, None)
self.assertEqual(season.type, 'select')

# i18n translations
def test_get_label_i18n_nl(self):
season = self.builder_i18n_nl.input_components['favouriteSeason']
Expand Down

0 comments on commit 45a1789

Please sign in to comment.